我有两个模特。Report和 Server,它们具有属于和具有多个关系。我使用 delegate创建了一个访问器方法,该方法允许 Report查找其关联的 Server.company_id。现在,我想在 Report上运行一个查询,它允许我找到与具有特定 company_id属性为5的特定 Server相关联的所有 Report。
这是我的两个模特。是的,我知道当前的查询不会工作,因为 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