Rails 3.1 HABTM 与自定义foreign_key 关联生成错误的连接语句
在尝试使用 has_and_belongs_to_many 关联时,我遇到了一个相当令人沮丧的问题。
场景如下。
我的产品有很多相关的新闻项目,反之亦然。新闻项目可以翻译成不同的语言,因此为了跟踪具有相同内容(但翻译成不同语言)的新闻 - 我在新闻中添加了 news_id。
我的问题是关联是在产品和唯一新闻(newsitem.news_id)之间,而不是在单个新闻项(newsitem.id)上。
我的模型:
class Product < ActiveRecord::Base
has_and_belongs_to_many :newsitems , :association_foreign_key => :news_id
end
class Newsitem < ActiveRecord::Base
has_and_belongs_to_many :products, :foreign_key => :news_id
end
我的迁移如下:
def change
create_table :products do |t|
t.string :name
t.timestamps
end
end
def change
create_table :newsitems do |t|
t.string :content
t.integer :news_id
t.integer :language_id
t.timestamps
end
end
def change
create_table :newsitems_products, :id => false do |t|
t.integer :news_id
t.integer :product_id
end
end
使用此设置,我在调用时生成以下正确的 sql:
news = Newsitem.first
news.products.to_sql
SQL:
"SELECT `products`.* FROM `products`
INNER JOIN `newsitems_products`
ON `products`.`id` = newsitems_products`.`product_id`
WHERE `newsitems_products`.`news_id` = 1"
当我询问与产品相关的所有新闻站点时,麻烦就开始了: 产品 = 产品.first prod.newsitems.to_sql SQL:
"SELECT `newsitems`.* FROM `newsitems`
INNER JOIN `newsitems_products`
ON `newsitems`.`id` = `newsitems_products`.`news_id`
WHERE `newsitems_products`.`product_id` = 1"
尽管我已经声明了 :association_foreign_key =>产品上的 :news_id 和 :foreign_key => :newsitem 上的news_id 生成“ON newsitems
.id
” 是错误的,应该是:
ON `newsitems`.`news_id` = `newsitems_products`.`news_id`
我希望你们中的一些人可以打开这个坚果。
提前致谢 - 彼得·派珀
I've encountered a rather frustrating problem when trying to use has_and_belongs_to_many associations.
The scenario is as follows.
I have a product that has many news items associated and vice versa. News items can be translated to different languages, so in order to keep track of news with same content (but translated to different language) - I've added a news_id to news.
My problem is that the association is between a product and a unique news (the newsitem.news_id) and not on the single news item (newsitem.id).
My models:
class Product < ActiveRecord::Base
has_and_belongs_to_many :newsitems , :association_foreign_key => :news_id
end
class Newsitem < ActiveRecord::Base
has_and_belongs_to_many :products, :foreign_key => :news_id
end
My migrations are as follows:
def change
create_table :products do |t|
t.string :name
t.timestamps
end
end
def change
create_table :newsitems do |t|
t.string :content
t.integer :news_id
t.integer :language_id
t.timestamps
end
end
def change
create_table :newsitems_products, :id => false do |t|
t.integer :news_id
t.integer :product_id
end
end
Using this setup I get the following correct sql generated when calling:
news = Newsitem.first
news.products.to_sql
SQL:
"SELECT `products`.* FROM `products`
INNER JOIN `newsitems_products`
ON `products`.`id` = newsitems_products`.`product_id`
WHERE `newsitems_products`.`news_id` = 1"
The troubles begin when I ask for all newsitems associated with product:
prod = Products.first
prod.newsitems.to_sql
SQL:
"SELECT `newsitems`.* FROM `newsitems`
INNER JOIN `newsitems_products`
ON `newsitems`.`id` = `newsitems_products`.`news_id`
WHERE `newsitems_products`.`product_id` = 1"
Eventhough I've declared :association_foreign_key => :news_id on product and :foreign_key => :news_id on newsitem the generate "ON newsitems
.id
" is wrong and should be:
ON `newsitems`.`news_id` = `newsitems_products`.`news_id`
I hope some of you can crack this nut open.
Thanks in advance - Peter Piper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到了同样的问题,我会写一个类似的例子。
1.第一个模型:
2.第二个模型:
3. 迁移:
编辑迁移:
问题:
我发现rails搜索了错误的表,它查找labelspaces而不是spaceslabels,我猜这是因为模型的名称。我通过添加模型解决了这个问题:
现在,您应该能够执行像 @space.labels 或 @label.spaces 这样的查询。
我希望它有帮助。
I had the same problem, I'll write a similar example.
1.First model:
2.Second model:
3. Migration:
Edit the migration:
Problems:
I found that rails searches the wrong table, it looks for labelsspaces instead of spaceslabels, I guess it's because the name of the models. I solved it by adding in the models:
Now, you should be able, to perform queries like @space.labels or @label.spaces.
I hope it helps.