打印

看敏捷开发遇到的问题

看敏捷开发遇到的问题

product = Product.create(:title => "Programming php?name=Ruby" onclick="tagshow(event)" class="t_tag">Ruby",
            :date_available => Time.now) 这一句已经提交到数据库,为什么还要下面这句
product.line_items.create是什么意思?

TOP

上面一句是创建产品,下面是创建订单项吧。

TOP

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 编辑 ]

TOP

2008-11-23 23:27 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61