2016-06-08 71 views
0

因此,我在我的Rails应用程序中构建了一个产品系统和一个购物车。我的目标是将购物车中保存的产品的id添加到用户模型中。因此,在我的购物车视图页面中,购物车中包含所有添加产品的列表,并且我想添加一个保存按钮,通过它们的ID将这些产品保存到用户表中的列中。举例来说,如果current_user在购物车中推出了三种产品,并且点击购物车中的“保存”按钮,我希望能够通过整数将这三个ID保存到三列:product_one,product_two ,current_user的product_three。如何将ID保存到用户列

到目前为止,这些都是我的模型:

class Item < ActiveRecord::Base 
    has_one :cart 
end 

class User < ActiveRecord::Base 

    has_one :cart 
    has_many :items, through: :cart 
end 

class Cart < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :item 

    validates_uniqueness_of :user, scope: :item 
end 

我的控制器:

class ItemsController < ApplicationController 
    before_action :set_item, only: [:show, :edit, :update, :destroy] 

    respond_to :html, :json, :js 

    def index 
    @items = Item.where(availability: true) 
    end 

    def show 
    end 

    def new 
    @item = Item.new 
    end 

    def edit 
    end 

    def create 
    @item = Item.new(item_params) 
    @item.save 
    respond_with(@item) 
    end 

    def update 
    @item.update(item_params) 
    flash[:notice] = 'Item was successfully updated.' 
    respond_with(@item) 
    end 

    def destroy 
    @item.destroy 
    redirect_to items_url, notice: 'Item was successfully destroyed.' 
    end 

    private 
    def set_item 
     @item = Item.find(params[:id]) 
    end 

    def item_params 
     params.require(:item).permit(:name, :description, :availability) 
    end 
end 

我的车控制:

class CartController < ApplicationController 

    before_action :authenticate_user!, except: [:index] 


    def add 
    id = params[:id] 
    if session[:cart] then 
     cart = session[:cart] 
    else 
     session[:cart] = {} 
     cart = session[:cart] 
    end 
    if cart[id] then 
     cart[id] = cart[id] + 1 
    else 
     cart[id] = 1 
    end 
    redirect_to :action => :index 
    end 


    def clearCart 
    session[:cart] = nil 
    redirect_to :action => :index 
    end 






    def index 
    if session[:cart] then 
     @cart = session[:cart] 
    else 
     @cart = {} 
    end 

    end 
end 

而且我使用的设计进行验证..

+0

很奇怪你的购物车belongs_to:item,不是吗?手段用户有一个手推车,并且手推车连接(属于)只有一个项目。所以用户只会有一个项目,对吗?您目前如何设想将多个项目连接到单个用户? –

+0

顺便说一下,通过将产品的id保存到显式列中的用户模型来解释您想要的结果。除了通过购物车从用户到物品的正常(关系)连接之外,您是否想要“非规范化”存储,即在用户表中具有项目ID的“缓存”?或者你想通过使用这种方法在用户和产品之间建立连接(即没有从用户到项目的其他方式,但只能通过这些列)。无论如何,因为你的建筑非常不合标准,而且从一开始看非常低效的连接方式,请解释你为什么要建造它。 –

+0

@PavelBulanov这可能是一个错误的方法。基本上我需要知道用户为了运送这些产品而选择了哪些产品。它可以是这些相关的任何方式,但我需要知道合并的用户标识和项目标识。只需要获取这些数据。 –

回答

2

我认为你可能误解了Rails的关系以及如何使用它们。由于定义关系的方法几乎是字面的,请仔细检查模型并“读取”它们。

  • 的项目都有一个购物车
  • 一车属于项目

是否有意义,一个项目有一个购物车?购物车有没有更多的意义,或有几个?

  • 一个车中有一个或多个项目
  • 一个项目属于车

然后,你就翻译成轨方法:

class User < ActiveRecord::Base 
    has_one :cart 
end 

class Cart < ActiveRecord::Base 
    belongs_to :user #carts table must have a user_id field 
    has_many :items 
end 

class Item < ActiveRecord::Base 
    belongs_to :cart #items table must have a cart_id field 
end 

现在,让我们回到文字。所以,如果我有一个user并想知道他在购物车中有什么物品,我该怎么办?

  • 我知道用户有一个购物车
  • 我知道,车中有一个或多个项目

因此,要恢复一个用户在一个购物车的物品:

user.cart.items 

并回答你原来的问题,如何将项目保存到user?你不需要。如果用户具有cart,并且该cart具有items,则自动user具有项目(如上所述通过cart访问它们)。

+0

谢谢!!这对我有很大帮助! :)欢呼声 –