我有2个模型。Report并且Server具有belongs_to 和has_many 关系。我创建了一个访问器方法,使用delegate它允许 aReport找到其关联的Server.company_id. 现在,我想运行一个查询Report,允许我找到与具有特定的特定Report关联的所有Servercompany_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