多个模型嵌套,数据关联的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yehkevin
V2EX    Ruby on Rails

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

  •  
  •   yehkevin 2013-12-20 10:34:32 +08:00 4252 次点击
    这是一个创建于 4366 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有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
    ese
    current_item = line_items.build(product_id: product_id)
    end
    current_item
    end

    #line_item
    belong_to :product
    belongs_to :product
    has_many :number

    #product
    has_many :line_itmes
    has_many :number

    #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

    在product 的show 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>,


    想咨询社区的高手帮忙看看这个要怎么做。谢谢
    3 条回复    1970-01-01 08:00:00 +08:00
    ygmpkk
        1
    ygmpkk  
       2013-12-20 12:43:15 +08:00
    大哥,你把代码贴到 https://gist.github.com
    yehkevin
        2
    yehkevin  
    OP
       2013-12-20 13:31:45 +08:00
    @ygmpkk 就把这些贴上去的吗
    ygmpkk
        3
    ygmpkk  
       2013-12-23 11:30:41 +08:00
    @yehkevin 对的,然后把url贴在这里,就可以自动显示出来。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1153 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 20ms UTC 23:32 PVG 07:32 LAX 15:32 JFK 18:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86