新手问题 多个模型嵌套,数据关联的问题

yeyong14 · 2013年12月20日 · 最后由 yeyong14 回复于 2013年12月20日 · 2529 次阅读

cart, line_item,和product, 以及一个number. 购物车按照 rails 敏捷开发做的,关系是一个产品有多个货号。当在选择产品的时候也同时可以选择多个货号。 相互的关系:

#cart
has_many :line_items
def add_product(product_id)
  current_item = line_items.find_by_product_id(product_id)
  if current_item
     current_item.quantity +=1
   else 
     current_item = line_items.build(product_id: product_id)
  end
  current_item
end

#line_item
belong_to :cart
belongs_to :product 
has_many :numbers

#product
has_many :line_itmes
has_many :numbers

#number
belong_to :line_item
belong_to :product

#product.show 控制器
def show
  @product = Product.find(params[:id])
  @numbers = @product.numbers
end 

#line_item 的控制器

def create
  product = Product.find(parms[:product_id])
  @line_item = @cart.add_product(product.id)
  if @line_item.save
    redirect_to cart_path
  end
end

productshow view 视图。

<%= form_for([@product, @product.line_items.build]) do |f| %>
  <% @numbers.each do |number| %>
    <li><%= check_box_tag 'line_item[number_ids][]', number.id,
            @line_item.numbers.map(&:id).include?(number.id) %>
         <%= number.number %>
   </li>
  <% end %>
  <%= f.hidden_field_tag 'line_item[number_ids][]', ' ' %>
  <%= f.submit %>
<% end %>

这样能添加数据

Parameters: {"utf8"=>"✓", "authenticity_token"=>"HCbW5r22boi1QrXPAT1as0EDKUiqxHQDjvM=", "line_item"=>{"number_ids"=>["28", "29", ""]}, "commit"=>"Create Line item", "locale"=>"en", "product_id"=>"2"}

但是没有添加进数据库,后来我想在控制器 create 中定义创建line_item

def create
  @product = Product_find(params[:product_id])
  @line_item = @cart.add_product(product.id)
  @line = @product.line_items.new(parms[:line_item])
  if @line_item.save && @line.save
    redirect_to cart_path
  end
end
Number Load (0.4ms)  SELECT `numbers`.* FROM `numbers` WHERE `numbers`.`line_item_id` = 101
 => [#<Number id: 26, number: "222", product_id: 6, created_at: "2013-12-19 09:20:29", updated_at: "2013-12-20 01:16:24", line_item_id: 101>, #<Number id: 27, number: "1111", product_id: 6, created_at: "2013-12-19 09:20:29", updated_at: "2013-12-20 01:16:24", line_item_id: 101>] 

这样就添加进数据库。但是这样以下子就创建了二个,而@line所创建的却没有关联购物车cart_id是空的。

#<LineItem id: 100, cart_id: 27, product_id: 6, created_at: "2013-12-20 01:16:24", updated_at: "2013-12-20 01:16:24", quantity: 1>,
#<LineItem id: 101, cart_id: nil, product_id: 6, created_at: "2013-12-20 01:16:24", updated_at: "2013-12-20 01:16:24", quantity: 1>,

想咨询社区的高手帮忙看看这个要怎么做。谢谢

首先,我没看过敏捷开发这本书,深深自觉挫爆了! 其次,至于为什么@line_item没有保存成功,可能你就要检查一下 log 输出或者调试一下@line_item.errors,另外检查一下 model 文件里边是不是定义了validatations,但是这些验证都没通过? 还有LineItemNumber的关系是不是反了?

最后,你都说高手了,还@我,让我压力好大!!!待会我被真的高手喷了怎么办?

@Martin91 我没有 model 加验证机制,保存都是成功的。只是在@line_item中没有添加 numbers_id. 我想在 line_item 的控制中,定义当添加进购物车的内容也同时添加 number_id,.这个有没有思路要怎么添加。像我在 create 中添加的

def create
  @product = Product_find(params[:product_id])
  @line_item = @cart.add_product(product.id)
  @line = @product.line_items.new(parms[:line_item])
  if @line_item.save && @line.save
    redirect_to cart_path
  end
end

这样就有二个 line_item 同时加进来,而且其中的一个没有 cart_id 是空的,不知道你有好的思路吗 在 cart 的模型中可以加的吗?

def add_product(product_id)
  current_item = line_items.find_by_product_id(product_id)
  if current_item
     current_item.quantity +=1
   else 
     current_item = line_items.build(product_id: product_id)
  end
  current_item
end

能给个思路要怎么加的吗

Rails 是什么版本?

代码最好仔细整理一下。下面这一块,真的是这两行都有么?

#line_item
belong_to :product
belongs_to :product 
has_many :number

这两个地方,number应该都是复数形式numbers吧?

#line_item
belong_to :product
belongs_to :product 
has_many :number

#product
has_many :line_itmes
has_many :number

@blueplanet 看的好仔细啊。打错了。rails 版本是 3.2.13

需要 登录 后方可回复, 如果你还没有账号请 注册新账号