Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails模型-attr\u访问器引发未知方法异常? 短版本(伪代码):_Ruby On Rails_Rails Activerecord_Rails Models - Fatal编程技术网

Ruby on rails Rails模型-attr\u访问器引发未知方法异常? 短版本(伪代码):

Ruby on rails Rails模型-attr\u访问器引发未知方法异常? 短版本(伪代码):,ruby-on-rails,rails-activerecord,rails-models,Ruby On Rails,Rails Activerecord,Rails Models,所以我有一个ActiveRecord(rails模型)类: 我明白了 undefined method `somevar=' for #<Class:0x007fa8cd0016a8> 和我的应用程序控制器 class ApplicationController < ActionController::Base around_filter :scope_current_user def scope_current_user User.curren

所以我有一个ActiveRecord(rails模型)类:

我明白了

undefined method `somevar=' for #<Class:0x007fa8cd0016a8>
和我的
应用程序控制器

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
undefined method `current_company=' for #<Class:0x007fa8cd0016a8>
如果手动定义方法,也会发生同样的情况:

def current_company
    @current_company
end
def current_company=(new_val)
    @current_company = new_val
end
这是不正确的:

User.current_company = current_user.company_id
attr\u accessor:current\u company
行将属性添加到用户实例,而不是用户类。您可以使用当前公司的访问器作为:

current_user.current_company = # whatever
您缺少的是它实际上使用的是
cattr\u accessible
,而不是
attr\u accessor
。因此,您的模型应该是:

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

如果这不起作用,请告诉我。

您确定
@u=User.find(1)
返回预期结果吗?我认为更大的问题是我不太理解
应用程序控制器中修改的代码会发生什么情况。我已经更新了我的问题并包含了实际的源代码。当然当我使用
cattr\u accessible
时,我得到
未定义的cattr\u accessible方法
:(因此,您最后添加的文本确实有效。但是,cattr_accessible引发了一个异常。您知道为什么吗?谢谢!@AndreasStorvikStrauman try
cattr_accessor
。不过,您最好还是使用手动定义的方法,因为它说它是线程安全的方法。
User.current_company = current_user.company_id
current_user.current_company = # whatever
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
def self.current_company
    @@current_company
end
def self.current_company=(new_val)
    @@current_company = new_val
end