0

我有这个架构

产品.rb:

has_many :families_products  
has_many :families, :through => :families_products

家庭.rb:

has_many :families_products  
has_many :products, :through => :families_products

家庭产品.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

我看到生成的查询有错误,所以有什么线索吗?

4

2 回答 2

0

发生这种情况是因为您的“family.rb”至少(可能还有“product.rb:”)配置错误。您是否定义了外键关系?系统如何猜测如何链接表?

于 2012-09-10T13:25:08.103 回答
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
于 2012-09-10T13:26:23.867 回答