Rails Model - attr_accessor raise unknown method exception? -


the short version (dummy code):

so have 1 activerecord (rails model) class:

class user < activerecord::base     attr_accessor :somevar end 

when

@u=user.find(1) @u.somevar = 1 

i

undefined method `somevar=' #<class:0x007fa8cd0016a8> 

i not have column in database called somevar
have restarted rails server after adding attr_accessor (just in case)
still getting bloody error.
have googled alot!

where can error be? thoughts?

i appriciate answers! thank you!


the long version (with actual code)

i use devise manage users. i'm trying add default condition, filter results, on of models. based on 'company_id' in user model. tried use session variable in default_scope , found this. says bad practice use session vars default conditions, , use devise , add modifications.

this resulted in user model be

class user < activerecord::base   # include default devise modules. others available are:   # :confirmable, :lockable, :timeoutable , :omniauthable   attr_accessor :current_company    devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable   belongs_to :company end 

and applicationcontroller

class applicationcontroller < actioncontroller::base   around_filter :scope_current_user      def scope_current_user         user.current_company = current_user.company_id     yield     ensure         #avoids issues when exception raised, clear current_id         user.current_company = nil          end   # prevent csrf attacks raising exception.   # apis, may want use :null_session instead.    protect_from_forgery with: :exception  end 

then raises, applicationcontroller

undefined method `current_company=' #<class:0x007fa8cd0016a8> 

the same happens if define methods manually:

def current_company     @current_company end def current_company=(new_val)     @current_company = new_val end 

this not correct:

user.current_company = current_user.company_id 

the attr_accessor :current_company line adds attribute user instances, not user class. use current_company accessor as:

current_user.current_company = # whatever 

the thing missing this url uses cattr_accessible , not attr_accessor. model should instaed be:

class user < activerecord::base   # include default devise modules. others available are:   # :confirmable, :lockable, :timeoutable , :omniauthable   cattr_accessible :current_company    devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable   belongs_to :company end 

this should work.

update based on last edit:

even when define methods manually, fall in same mistake: defining instance methods , trying call them class methods. should define them class methods, done adding self. before names of methods, so:

def self.current_company     @@current_company end def self.current_company=(new_val)     @@current_company = new_val end 

please tell me if doesn't work.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -