Rails 3.1 HABTM 与自定义foreign_key 关联生成错误的连接语句

发布于 2024-12-26 02:56:28 字数 1821 浏览 0 评论 0原文

在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2025-01-02 02:56:28

我也遇到了同样的问题,我会写一个类似的例子。

1.第一个模型:

class Label < ActiveRecord::Base
 has_and_belongs_to_many :spaces
end

2.第二个模型:

class Space < ActiveRecord::Base
 has_and_belongs_to_many :labels
end

3. 迁移:

rails g migration createSpacesLabels
  1. 编辑迁移:

    class CreateSpacesLabels <; ActiveRecord::迁移
     确定
      创建表:空间标签,:id =>假|t|
      t.参考文献:空间
      t.参考文献:标签
     结尾
     add_index :spaces_labels, [:label_id, :space_id]
     add_index :spaces_labels, [:space_id, :label_id]
    结尾
    
    绝对向下
     drop_table :spaces_labels
     结尾
    结尾
    
  2. 问题:

    我发现rails搜索了错误的表,它查找labelspaces而不是spaceslabels,我猜这是因为模型的名称。我通过添加模型解决了这个问题:

    has_and_belongs_to_many :labels,:join_table=>'spaces_labels'
    
    has_and_belongs_to_many :spaces,:join_table=>'spaces_labels'
    

现在,您应该能够执行像 @space.labels 或 @label.spaces 这样的查询。
我希望它有帮助。

I had the same problem, I'll write a similar example.

1.First model:

class Label < ActiveRecord::Base
 has_and_belongs_to_many :spaces
end

2.Second model:

class Space < ActiveRecord::Base
 has_and_belongs_to_many :labels
end

3. Migration:

rails g migration createSpacesLabels
  1. Edit the migration:

    class CreateSpacesLabels < ActiveRecord::Migration
     def up
      create_table :spaces_labels, :id => false do |t|
      t.references :space
      t.references :label
     end
     add_index :spaces_labels, [:label_id, :space_id]
     add_index :spaces_labels, [:space_id, :label_id]
    end
    
    def down
     drop_table :spaces_labels
     end
    end
    
  2. 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:

    has_and_belongs_to_many :labels,:join_table=>'spaces_labels'
    
    has_and_belongs_to_many :spaces,:join_table=>'spaces_labels'
    

Now, you should be able, to perform queries like @space.labels or @label.spaces.
I hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文