开发者

Ruby on Rails, apply check to a column in table

开发者 https://www.devze.com 2023-02-20 07:42 出处:网络
i have a problem that i can\'t seem to get out of, im simply trying to apply a check to my shopping cart that if the value in the stock column is less then 1 then don\'t add the item into the instead

i have a problem that i can't seem to get out of, im simply trying to apply a check to my shopping cart that if the value in the stock column is less then 1 then don't add the item into the instead print a msg saying something like out of stock. my coad is below

 def create
   prod_id = params[:line_item][:product_id]
   curnt_itm = @line_items.find(:first, :conditions => "product_id = #{prod_id}")
   if (curnt_itm.stock > 0)
     @line_item = @cart.add_product(params[:line_item][:product_id], params[:line_item][:quantity])
  respond_to do |format|
    if @line_item.save
      format.html { redirect_to(@line_item.cart, :notice => '') }
      format.xml  { render :xml => @line_item, :status => :created, :location => @line_item }
    else
      format.html { render :action => "new" }
      format.xml  { render开发者_Python百科 :xml => @line_item.errors, :status => :unprocessable_entity }
    end
  end
end

end

this creates a new line item, but everytime i try to execute this it gives an error msg saying Wrong number of arguments 2 for 1. it in the line

curnt_itm = @line_items.find(:first, :conditions => "product_id = #{prod_id}")

can anyone help me sort out the problem, thanks for any help...


You should use class methods instead of instance. For example:

LineItem.where("product_id = ?", prod_id).first

Can you provide some info about @line_items?

May be it should be like:

 curnt_itm = @line_items.class.find(:first, :conditions => ["product_id = ?", prod_id])

or

 curnt_itm = @line_items.class.where("product_id = ?", prod_id).first
0

精彩评论

暂无评论...
验证码 换一张
取 消