简体   繁体   English

Rails模型-attr_accessor引发未知方法异常?

[英]Rails Model - attr_accessor raise unknown method exception?

The short version (dummy code): 简短版本(虚拟代码):

So I have one ActiveRecord (rails model) class: 所以我有一个ActiveRecord(rails模型)类:

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 我在数据库中没有名为somevar的列
I have restarted rails server after adding attr_accessor (just in case) 添加attr_accessor后,我已经重新启动了rails server (以防万一)
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. 我使用Devise管理用户。 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. 这基于用户模型中的“ company_id”。 I tried to use the session variable in default_scope and found this . 我尝试在default_scope使用会话变量,并找到了 It basically says that it is bad practice to use session vars to default conditions, and I could use Devise and add some modifications. 基本上说,将会话变量用于默认条件是一种不好的做法,我可以使用Devise并添加一些修改。

This resulted in my User model to be 这导致我的User model成为

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 还有我的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 然后从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

This is not correct: 这是不正确的:

User.current_company = current_user.company_id

The attr_accessor :current_company line adds an attribute to User instances , not the User class . attr_accessor :current_company行将属性添加到User 实例 ,而不是User You could use the current_company accessor as: 您可以将current_company访问器用作:

current_user.current_company = # whatever

The thing you are missing from this url is that it actually uses cattr_accessible and not attr_accessor . 您从此url中缺少的是它实际上使用cattr_accessible而不是attr_accessor So your model should instaed be: 因此,您的模型应该为:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :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 you define the methods manually, you fall in the same mistake: you are defining instance methods and trying to call them as class methods. 即使手动定义方法,也会犯同样的错误:定义实例方法并尝试将它们称为类方法。 You should define them as class methods, which is done by adding self. 您应该将它们定义为类方法,这是通过添加self.来完成的self. before the names of the methods, like so: 在方法名称之前,如下所示:

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

Please tell me if this doesn't work. 请告诉我这是否无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM