The short version (dummy code):
So I have one ActiveRecord (rails model) class:
class User < ActiveRecord::Base
attr_accessor :somevar
end
When I do
@u=User.find(1)
@u.somevar = 1
I get
undefined method `somevar=' for #<Class:0x007fa8cd0016a8>
I do not have a column in the database called somevar
I have restarted rails server
after adding attr_accessor
(just in case)
Still getting the bloody error.
Have googled alot!
Where can the error be? Any thoughts?
I appriciate all answers! Thank you!
The long version (with the actual code)
I use Devise to manage my users.
Also I'm trying to add a default condition, to filter the results, on some of the models. This is based on 'company_id' which is in the User model. I tried to use the session variable in default_scope
and found this. It basically says that it is bad practice to use session vars to default conditions, and I could use Devise and add some modifications.
This resulted in my User model
to be
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
attr_accessor :current_company
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :company
end
And my 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 an exception is raised, to clear the current_id
User.current_company = nil
end
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
Then it raises, from the ApplicationController
undefined method `current_company=' for #<Class:0x007fa8cd0016a8>
The same happens if I define the methods manually:
def current_company
@current_company
end
def current_company=(new_val)
@current_company = new_val
end