【问题标题】:Ruby on Rails: ActiveRecord::StatementInvalid can't cast ActiveRecord::StatementInvalidRuby on Rails:ActiveRecord::StatementInvalid 无法转换 ActiveRecord::StatementInvalid
【发布时间】:2017-05-03 01:06:02
【问题描述】:

我有这个 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

我需要产品的 ID。 第 3 行是错误的。

我有一个MemberProduct 表,其中有一个product_id 字段、一个expiration_date 字段和一个member_id 字段(current_member 来自devise) 我有一个Product 表,其中有一个barcode_number 字段和一个name 字段。

我收到此错误:

ActiveRecord::StatementInvalid in FoodController#newfood TypeError: 无法将 ActiveRecord::Relation::ActiveRecord_Relation_Product 转换为 字符串:INSERT INTO "member_products" ("created_at", “expiration_date”、“member_id”、“product_id”、“updated_at”)值(?, ?, ?, ?, ?)

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby class


    【解决方案1】:

    试试

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

    memberproduct.product_id 是 Rails 存储与您的 memberproduct 关联的产品 ID 的数据库列。通常,这些不直接使用;相反,关联名称是。

    所以这两个都有效:

    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
    

    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
    

    但第一种形式更常见。如果您将 productmember 之类的对象分配给关联,Rails 会很聪明地询问对象的 ID 并自动使用它。

    另外,Product.where 可能会返回多个结果。由于您只期望一个,添加 .first 以仅返回第一个匹配项。

    【讨论】:

    • 谢谢,如果我必须检索此产品的 id 怎么办?将更新问题。
    • 我不确定我是否理解 - 您确实拥有该产品。如果你真的需要模型ID,你可以用current_member.id做你所做的,在.first之后添加.id
    • 这可能是一个愚蠢的错误,但我现在有这个代码:'product = Product.where(:barcode_number => params[:barcode_number]).first memberproduct = MemberProduct.new memberproduct.product_id = product .id memberproduct.expiration_date = params[:expiration_date] memberproduct.member_id = current_member.id memberproduct.save' 它说 nil:NilClass 的未定义方法 `id' 但是已经谢谢了!
    • productcurrent_member 哪个是 nil?如果是product,则表示您的Product.where(:barcode_number => params[:barcode_number]) 查询没有找到产品。
    • 没有找到产品,哈哈。我不知道为什么,但不知怎的,我把 0034000098279 写成了 34000098279。谢谢你的帮助!
    【解决方案2】:

    根据 Rails 的版本,您应该能够:

    导轨 3: Product.find_by_barcode_number(params[:barcode_number])

    导轨 4: Product.find_by(barcode_number: params[:barcode_number])

    您可以这样简化您的操作:

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

    尽管您可能想要处理验证等 (if mprod.save .. else)

    【讨论】:

    • 我已经找到了,但感谢您格式化问题!如果我的声望超过 15 个,我肯定会投赞成票,哈哈。
    • @xprise 哈哈!不用担心。
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2014-02-08
    • 2013-10-31
    • 2014-02-03
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多