【发布时间】:2011-09-24 10:06:05
【问题描述】:
我正在编写一个简单的会计系统来管理成本。结构是这样的:
Invoice - can have many products
Product - can have many costs, also can act_as_tree
LineItem -
Product
LineItem
LineItem
Product
LineItem
Product
LineItem
LineItem
我将这设置为三个类的 has_many 和 belongs_to 但认为以下更合适(基于阅读 Rails 3 Way - 任何缺点都是我缺乏理解;只是试图给出上下文)
Class Invoice < ActiveRecord::Base
has_many :products
has_many :line_items, :through => :products
end
Class Product < ActiveRecord::Base
belongs_to :invoice
belongs_to :line_item
end
class LineItem < ActiveRecord::Base
has_many :products
has_many :invoices, :through => :invoices
end
但我不认为这是正常工作。
如果我执行以下操作:
>@i=Invoice.find(1)
>@i.products # this works
>@i.line_items # doesn't work, unidentified method line_items
这是我第一次使用 has_many :through。这是否为我的数据模型正确设置?另外,是否可以将它与acts_as_tree 结合使用-我想说:
>@i.line_items
并取回该特定发票的所有行项目。可能吗?
感谢帮助
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord