Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the what is the field type of a field in rails 3

I have a user model. like

class User < ActiveRecord::Base
  attr_accessor :username :campaign_id
  attr_accessible :name, :email, :username
end

if i fetch one user record like

u = User.first

it will return

#<User id: 190, name: "sa", :email: "[email protected]", username: "ab">

So the question is: Is there any way to check whether one attribute e.g. 'name' is a attr_accessor or attr_accessible?

like image 294
Sabyasachi Ghosh Avatar asked Oct 02 '22 22:10

Sabyasachi Ghosh


2 Answers

If I understand correctly, the devise attr_accessor :password hides your password field in the database. To circumvent this, you could do something like

def raw_password
  self[:password]
end

def raw_password= (new_password)
  self[:password] = new_password
end

Of course, this is completely disregarding the fact that is not very safe to store the password in cleartext in the database. Devise offers enough mechanisms for users to manage their own password, so you should not be keeping the raw password for much longer.

like image 103
nathanvda Avatar answered Oct 07 '22 17:10

nathanvda


Perhaps not the best solution, but by looking at the source code I guess you could check if :name appears in the _accessible_attributes field of your class or not.

like image 41
Felix Avatar answered Oct 07 '22 19:10

Felix