我有2个模型。Report并Server具有belongs_to和has_many关系。我使用创建了一个访问器方法delegate,该方法允许a Report查找与其关联的Server.company_id。现在,我要运行一个查询Report,使我能够查找Report与Server具有特定company_id属性5 的特定对象相关联的所有对象。
这是我的两个模型。是的,我知道当前查询将不起作用,因为Report它没有属性company_id。
不,我不希望在其中存储信息company_id,Report因为该信息不属于Report。
报告
class Report < ActiveRecord::Base belongs_to :server delegate :company_id, :to => :server class << self def method(url, base_url) #Report.where(company_id: 5) end end end
服务器
class Server < ActiveRecord::Base attr_accessible :company_id has_many :reports end