2016-08-02 165 views
0

我想通过值的关联模型来过滤我的模型。 (我想通过商店,这是产品的一个协会,来筛选变种ActiveRecord :: StatementInvalid与SQLite3 :: SQLException:没有这样的列

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: 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" = ?)

为什么我收到此错误?STORE_ID肯定是我的产品表中的列。

回答

1

表的定义,但因为它是所谓products,就无法找到一个product前缀的列,你可以在你的方法使用where('products.store_id = ?', store_id)

+0

这会引发相同的错误。 '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”=?):' – stoerebink

+0

Woops。把这个错误的方法。这解决了我的问题!非常感谢!! – stoerebink

相关问题