简体   繁体   English

Ruby on Rails:ActiveRecord :: StatementInvalid无法强制转换ActiveRecord :: StatementInvalid

[英]Ruby on Rails: ActiveRecord::StatementInvalid can't cast ActiveRecord::StatementInvalid

I have this Rails code: 我有这个Rails代码:

def newfood
  memberproduct = MemberProduct.new
  memberproduct.product_id = Product.where(:barcode_number => params[:barcode_number]).id
  memberproduct.expiration_date = params[:expiration_date]
  memberproduct.member_id = current_member.id
  memberproduct.save
end

I need the id of the product. 我需要产品的ID。 The 3rd line is wrong. 第3行错了。

I have a MemberProduct table with a product_id field, an expiration_date field and a member_id field (current_member comes from devise ) I have a Product table with a barcode_number field and a name field. 我有一个带有product_id字段的MemberProduct表,一个expiration_date字段和一个member_id字段(current_member来自devise )我有一个带有barcode_number字段和name字段的Product表。

I get this error: 我收到此错误:

ActiveRecord::StatementInvalid in FoodController#newfood TypeError: can't cast ActiveRecord::Relation::ActiveRecord_Relation_Product to string: INSERT INTO "member_products" ("created_at", "expiration_date", "member_id", "product_id", "updated_at") VALUES (?, ?, ?, ?, ?) FoodController中的ActiveRecord :: StatementInvalid #newfood TypeError:无法将ActiveRecord :: Relation :: ActiveRecord_Relation_Product转换为字符串:INSERT INTO“member_products”(“created_at”,“expiration_date”,“member_id”,“product_id”,“updated_at”)价值观(?,?,?,?,?)

What am I doing wrong? 我究竟做错了什么?

Try 尝试

memberproduct.product = Product.where(:barcode_number => params[:barcode_number]).first

memberproduct.product_id is the database column where Rails stores the ID of the product associated to your memberproduct. memberproduct.product_id是数据库列,其中Rails存储与您的memberproduct关联的产品的ID。 Usually, these are not used directly; 通常,这些不是直接使用的; instead, the association name is. 相反,关联名称是。

So both of these work: 所以这两个工作:

def newfood
  memberproduct                 = MemberProduct.new
  product                       = Product.where(:barcode_number => params[:barcode_number]).first
  memberproduct.product         = product
  memberproduct.expiration_date = params[:expiration_date]
  memberproduct.member          = current_member

  memberproduct.save
end

and

def newfood
  memberproduct                 = MemberProduct.new
  product                       = Product.where(:barcode_number => params[:barcode_number]).first
  memberproduct.product_id      = product.id
  memberproduct.expiration_date = params[:expiration_date]
  memberproduct.member_id       = current_member.id

  memberproduct.save
end

but the first form is more common. 但第一种形式更常见。 If you assign an object like product or member to the association, Rails is smart enough to ask the object for its ID and use it automatically. 如果您将对象(如productmember分配给关联,则Rails足够聪明,可以向对象询问其ID并自动使用它。

Also, Product.where potentially returns multiple results. 此外, Product.where可能会返回多个结果。 Since you only expect one, add .first to return only the first match. 因为你只想要一个,所以添加.first只返回第一个匹配。

Depending on version of Rails, you should be able to: 根据Rails的版本,您应该能够:

Rails 3: Product.find_by_barcode_number(params[:barcode_number]) Rails 3: Product.find_by_barcode_number(params[:barcode_number])

Rails 4: Product.find_by(barcode_number: params[:barcode_number]) Rails 4: Product.find_by(barcode_number: params[:barcode_number])

You can simplify your action as so: 您可以这样简化操作:

mprod = current_member.build_member_product(expiration_date: params[:expiration_date])
mprod.product = Product.find_by(barcode_number: params[:barcode_number])
mprod.save

Though you probably want to deal with validations and such ( if mprod.save .. else ) 虽然您可能想要处理验证等( if mprod.save .. else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM