我有这个架构
product.rb:
has_many :families_products
has_many :families, :through => :families_products
family.rb:
has_many :families_products
has_many :products, :through => :families_products
families_product.rb:
belongs_to :product
belongs_to :family
并且在families_product
表中我有一个名为price
的属性,当我在创建后尝试更新它时会抛出错误。
1.9.3p0 :027 > family_product = FamiliesProduct.first
FamiliesProduct Load (0.9ms) SELECT `families_products`.* FROM `families_products` LIMIT 1
=> #<FamiliesProduct family_id: 1, product_id: 1, created_at: "2012-09-10 12:31:54", updated_at: "2012-09-10 12:31:54", points: nil>
1.9.3p0 :028 > family_product.points = 2
=> 2
1.9.3p0 :029 > family_product.save
(0.2ms) BEGIN
(0.7ms) UPDATE `families_products` SET `points` = 2, `updated_at` = '2012-09-10 12:53:05' WHERE `families_products`.`` IS NULL
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'families_products.'
in 'where clause': UPDATE `families_products` SET `points` = 2,
`updated_at` = '2012-09-10 12:53:05' WHERE `families_products`.`` IS NULL
我看到生成的查询有错误,所以任何线索?
答案 0 :(得分:0)
这是因为您的“family.rb”至少(可能是“product.rb:”)配置错误。你定义了外键关系吗?系统如何猜测如何链接表?
答案 1 :(得分:0)
我认为你不应该像这样使用连接表模型。 试试这个:
@family = Family.first #or some other family
families_product = @family.families_products.where({:product_id => Product.first.id})
families_product.points = 2
families_product.save