从关联方法中访问Rails proxy_association.owner



是否有方法从关联的方法中访问proxy_association对象?

示例:

class User < ActiveRecord:Base
  has_many :accounts
end
class Account < ActiveRecord:Base
  belongs_to :user
  def some_function
    # Here I want to access the same user object the association was called on
    # (that holds all already defined global variables), not a newly created object
    # through self.user (where all global variables are reset).
    proxy_association.owner
  end
end

如何从关联中访问调用关联的对象不幸的是,self.user返回一个新对象,其中所有先前设置的变量都恢复为默认值。

has_many关联上使用:inverse_of选项。这将在使用关联时连接内存中的两个模型。

class User < ActiveRecord:Base
  has_many :accounts, inverse_of: :user
end

来源:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many

相关内容

  • 没有找到相关文章

最新更新