【问题标题】:ActiveRecord::StatementInvalid with SQLite3::SQLException: no such columnActiveRecord::StatementInvalid with SQLite3::SQLException: no such column
【发布时间】:2016-12-08 06:10:42
【问题描述】:

我正在尝试按关联模型的值过滤我的模型。 (我正在尝试按商店过滤我的变体,这是产品的关联。

class Variant < ActiveRecord::Base
belongs_to :product

def self.by_store_id(store_id)
    where(:product => {:store_id => store_id})
end

### 
class Product < ActiveRecord::Base
belongs_to :store

class Store < ActiveRecord::Base
has_many :products

每次我尝试这个,我都会收到这个错误:

ActiveRecord::StatementInvalid (SQLite3::SQLException: 没有这样的列: product.store_id: SELECT COUNT(*) FROM "variant_skus" INNER JOIN “产品”在“产品”上。“id”=“variant_skus”。“product_id”在哪里 "product"."store_id" = ?)

为什么会出现此错误? Store_id 绝对是我产品表上的一列。

【问题讨论】:

    标签: ruby-on-rails activerecord sqlite


    【解决方案1】:

    该表已定义,但由于它被称为products,它将无法找到带有product 前缀的列。您可以在方法中使用where('products.store_id = ?', store_id)

    【讨论】:

    • 这会引发同样的错误。 ActiveRecord::StatementInvalid (SQLite3::SQLException: no such column: product.store_id: SELECT COUNT(*) FROM "variant_skus" INNER JOIN "products" ON "products"."id" = "variant_skus"."product_id" WHERE "product"."store_id" = ?):
    • 糟糕。把它放在错误的方法中。这解决了我的问题!非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    相关资源
    最近更新 更多