如何创建遍历多态关联的作用域?
我有这些模型(伪代码):
class Order
has_many :line_items
end
class LineItem
belongs_to :purchasable, :polymorphic => true
belongs_to :order
end
class Tile
has_one :line_item, :as => :purchasable
end
我想创建一个范围,允许我从订单访问图块。像 Order#tiles
之类的东西,这样我就可以在控制器中执行类似的操作:
my_order.tiles.new(...)
my_order.tiles.find(params[:id]).update_attributes(...)
如何构建这样的范围? (或者我应该使用其他技术吗?)
I have these models (psuedocode):
class Order
has_many :line_items
end
class LineItem
belongs_to :purchasable, :polymorphic => true
belongs_to :order
end
class Tile
has_one :line_item, :as => :purchasable
end
I want to make a scope that allows me to access tiles from an order. something like Order#tiles
so that I can do things like this in controllers:
my_order.tiles.new(...)
my_order.tiles.find(params[:id]).update_attributes(...)
How can I construct such a scope? (or is there another technique I should use?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你们所拥有的协会不能一起工作。我想你可能正在寻找这样的东西:
The associations you have don't work together. I think you might be looking for something like this: