【问题标题】:Rails3 - many_to_many relationships and scope chainingRails3 - many_to_many 关系和范围链
【发布时间】:2011-05-22 14:01:45
【问题描述】:

假设我在文章和标签之间有一个多对多关系

class ArticleTag < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag
end

class Tag < ActiveRecord::Base
  has_many :article_tags
  has_many :articles, :through => :article_tags
end

class Article < ActiveRecord::Base
  has_many :article_tags
  has_many :tags, :through => :article_tags

  named_scope :tagged, lambda { |id| joins(:tags).where("tags.id = ?", id) }
end

文章具有标记的范围,顾名思义,它允许我检索带有特定标签的文章

困扰我的是:

$ a = Article.create
 => #<Article id: 3, created_at: "2011-05-22 13:54:02", updated_at: "2011-05-22 13:54:02"> 
$ t1 = Tag.create
 => #<Tag id: 4, created_at: "2011-05-22 13:54:07", updated_at: "2011-05-22 13:54:07"> 
$ t2 = Tag.create
 => #<Tag id: 5, created_at: "2011-05-22 13:54:11", updated_at: "2011-05-22 13:54:11"> 
$ a.tags << t1
 => [#<Tag id: 4, created_at: "2011-05-22 13:54:07", updated_at: "2011-05-22 13:54:07">] 
$ a.tags << t2
 => [#<Tag id: 4, created_at: "2011-05-22 13:54:07", updated_at: "2011-05-22 13:54:07">, #<Tag id: 5, created_at: "2011-05-22 13:54:11", updated_at: "2011-05-22 13:54:11">] 
$ Article.tagged(t1.id)
 => [#<Article id: 3, created_at: "2011-05-22 13:54:02", updated_at: "2011-05-22 13:54:02">] 
$ Article.tagged(t2.id)
 => [#<Article id: 3, created_at: "2011-05-22 13:54:02", updated_at: "2011-05-22 13:54:02">] 
$ Article.tagged(t1.id).tagged(t2.id)
 => [] 

如果一篇文章被标记了两个标签,链接相应的范围不允许它被检索。这是假定的行为吗?如果是,我应该如何更改我的代码以使最后一行不返回空数组?

PS : 这是生成的 SQL。

SELECT \"articles\".* FROM \"articles\" INNER JOIN \"article_tags\" ON \"articles\".\"id\" = \"article_tags\".\"article_id\" INNER JOIN \"tags\" ON \"tags\".\"id\" = \"article_tags\".\"tag_id\" WHERE (tags.id = 4) AND (tags.id = 5)

【问题讨论】:

标签: sql ruby-on-rails activerecord many-to-many named-scope


【解决方案1】:

虽然我不知道为什么内置链接失败,但这里有一个解决方案:

在您的 Article 模型中

def self.tagged_by_one_in(*ids)
  return [] if ids.nil?
  self.joins(:tags).where("tags.id in (?)", ids).group(:article_id)
end

def self.tagged_by_all_in(*ids)
  return [] if ids.nil?
  #sorry raw sql there, found no Rails way.
  self.find_by_sql("select * from articles where id in (select article_id from article_tags where tag_id in (" + ids * ", " + ") group by article_id having count(*) = " + ids.size.to_s + ")" )
end

在控制台中,你可以调用这些方法:

Article.tagged_by_one_in(1,2)
Article.tagged_by_all_in(1,2)

【讨论】:

    【解决方案2】:

    试试这个

    在 Article.rb 模型中

    named_scope :tagged, lambda { |ids| joins(:tags).where("tags.id in (?)", ids) }
    

    在控制台中

    $ Article.tagged([t1.id,t2.id])
    

    【讨论】:

    • 如果他想检索所有标签都通过的文章,这并不能回答问题
    【解决方案3】:

    好的,我找到了解决方案。诀窍是手动创建连接,并在某些地方添加唯一 id 以避免名称冲突。

    named_scope :tagged, lambda { |id| uid = rand(36**8).to_s(36); joins("INNER JOIN article_tags AS at_#{uid} ON (articles.id = at_#{uid}.article_id) INNER JOIN tags AS tags_#{uid} ON (tags_#{uid}.id = at_#{uid}.tag_id)").where("tags_#{uid}.id = ?",id) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多