使用通用名称引用相似的关联模型
我有这些模型:
class Bill < ActiveRecord::Base
has_many :calls
has_many :text_messages
end
class Call < ActiveRecord::Base
belongs_to :bill
end
class TextMessage < ActiveRecord::Base
belongs_to :bill
end
现在,在我的域中,通话和短信都是“同一类事情”——即,它们都是“账单项目”。因此,我希望 some_bill.bill_items
返回与该账单相关的所有来电和短信。最好的方法是什么?
I have these models:
class Bill < ActiveRecord::Base
has_many :calls
has_many :text_messages
end
class Call < ActiveRecord::Base
belongs_to :bill
end
class TextMessage < ActiveRecord::Base
belongs_to :bill
end
Now, in my domain calls and text messages are both "the same kind of thing" -- i.e., they're both "bill items". So I'd like some_bill.bill_items
to return all calls and text messages associated with that bill. What's the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个新模型
BillItems
并将其插入到您的关系链中。然后,您可以通过多对多关系来集体访问所有账单项目或单独的呼叫/短信。另一种可能性是研究单表继承。You could create a new model,
BillItems
and insert it into your relationship chain. Then you could access all bill items collectively or individual call/text messages via a has many through relationship. Another possibility would be to look into single table inheritance.