我有两个模型.Report
和Server
有一个属于并且有很多关系.我使用delegate
创建了一个访问器方法,它允许Report
查找其关联的Server.company_id
.现在,我想在Report
上运行一个查询,它允许我找到所有与特定Server
相关联的Report
,该Server
的特定company_id
属性为5.
这是我的两个模型.是的,我知道当前的查询不起作用,因为Report
没有属性company_id
.
不,我不想在Report
中存储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
Server
class Server < ActiveRecord::Base
attr_accessible :company_id
has_many :reports
end