product 和 line_item的关系是这样的.
class Product < ActiveRecord::Base
has_many :line_items
...
end
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end书上是对创建子对象并链接到父对象的两种方式进行比较
1.
line_item = LineItem.new
line_item.product = product
line_item.save
...
2.
product.line_items.create
说明第2种方式建好的line_items的size是正确的1, 而第1种手工链接的时候, size一开始还是0, 经过refresh后才是1.
[
本帖最后由 5swords 于 2008-4-1 09:02 编辑 ]