Rails has_many :通过 "ERROR - column products.category does not exist"
Rails has_many :through "ERROR - column products.category does not exist"
在尝试解决我的问题时,我发现围绕 has_many :through
和 has_and_belongs_to_many
(我想避免使用)有很多常见的混淆。通读 Whosebug 上的大部分内容让我觉得我已经正确设置了它:
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
还有我的迁移。我见过几种解决此问题的方法,这种方法似乎有效:
class CreateCategorizations < ActiveRecord::Migration
def change
create_table :categorizations, :id => false do |t|
t.references :product
t.references :category
end
add_index :categorizations, [:category_id, :product_id], :unique => true
end
end
在我的类别控制器中,我试图通过此关联创建一个 ActiveRecord 关系:
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
@products = Product.where(:category => @category)
end
end
如果我只是 @category.products
,一切都很好,并且我得到了该类别中包含的许多产品的 array/collection。但我不想要一个数组。如果我尝试使用 where
我会得到这个错误:
ActiveRecord::StatementInvalid - PG::UndefinedColumn: ERROR: column products.category does not exist
我是否应该使用更复杂的连接语句来获取特定类别产品的 AR 关系?我确定它应该按照我尝试的方式工作,但显然我在某处出错了。
更新
我已经能够使用此代码创建关系:
@products = Product.where(:id => @category.product_ids)
这是正确的方法吗?我仍然觉得我的 "column does not exist error" 表明我在其他地方做错了一些事情,需要识别它。
ActiveRecord::StatementInvalid - PG::UndefinedColumn: ERROR: column
products.category does not exist
该错误表明 列类别在产品 table 中不存在。这是因为您试图在此行 @products = Product.where(:category => @category)
中使用不存在的列进行查询
此外,我会简单地将其写为 @products = @category.products
,但正如您希望结果为 AR 关系 您当前的approach(@products = Product.where(:id => @category.product_ids)
) 对我来说很好。
在尝试解决我的问题时,我发现围绕 has_many :through
和 has_and_belongs_to_many
(我想避免使用)有很多常见的混淆。通读 Whosebug 上的大部分内容让我觉得我已经正确设置了它:
class Product < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :products, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
还有我的迁移。我见过几种解决此问题的方法,这种方法似乎有效:
class CreateCategorizations < ActiveRecord::Migration
def change
create_table :categorizations, :id => false do |t|
t.references :product
t.references :category
end
add_index :categorizations, [:category_id, :product_id], :unique => true
end
end
在我的类别控制器中,我试图通过此关联创建一个 ActiveRecord 关系:
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
@products = Product.where(:category => @category)
end
end
如果我只是 @category.products
,一切都很好,并且我得到了该类别中包含的许多产品的 array/collection。但我不想要一个数组。如果我尝试使用 where
我会得到这个错误:
ActiveRecord::StatementInvalid - PG::UndefinedColumn: ERROR: column products.category does not exist
我是否应该使用更复杂的连接语句来获取特定类别产品的 AR 关系?我确定它应该按照我尝试的方式工作,但显然我在某处出错了。
更新
我已经能够使用此代码创建关系:
@products = Product.where(:id => @category.product_ids)
这是正确的方法吗?我仍然觉得我的 "column does not exist error" 表明我在其他地方做错了一些事情,需要识别它。
ActiveRecord::StatementInvalid - PG::UndefinedColumn: ERROR: column products.category does not exist
该错误表明 列类别在产品 table 中不存在。这是因为您试图在此行 @products = Product.where(:category => @category)
此外,我会简单地将其写为 @products = @category.products
,但正如您希望结果为 AR 关系 您当前的approach(@products = Product.where(:id => @category.product_ids)
) 对我来说很好。