2011-10-30 139 views
0

我有以下型号:Rails has_many关系中的多个INNER JOIN?

class Image < ActiveRecord::Base 
    belongs_to :gallery 
    has_many :bookmarks 
    has_many :gallery_tags, :foreign_key => :gallery_id 
end 

class Bookmark < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :image 
    has_many :gallery_tags, :through => :image, :source => :gallery_tags 
end 


class GalleryTag < ActiveRecord::Base 
    belongs_to :gallery 
    belongs_to :tag 
end 

class Gallery < ActiveRecord::Base 
    belongs_to :provider 
    has_many :images 
    belongs_to :user 
    has_many :gallery_tags 
    has_many :tags, :through => :gallery_tags 
end 

class Tag < ActiveRecord::Base 
end 

class User < ActiveRecord::Base 
    has_many :bookmarks 
    has_many :galleries 
end 

我希望能够做到

User.find(1).bookmarked_tags 

和检索通过与图像相关的画廊与用户的所有书签的图像相关联的所有标签。标签与画廊相关联。

查询最终会看起来像这样:

SELECT 
    tags.* 
FROM 
    tags 
    INNER JOIN gallery_tags ON gallery_tags.tag_id = tags.id 
    INNER JOIN images ON gallery_tags.gallery_id = images.gallery_id 
    INNER JOIN bookmarks ON images.id = bookmarks.image_id 
WHERE 
    bookmarks.user_id = 1 
GROUP BY 
    tags.id; 

我已经尝试添加

has_many :tags, :through => :gallery_tags, :foreign_key => :gallery_id 

到图像时,这会导致Image.find(34).tags添加导致

SELECT `tags`.* FROM `tags` INNER JOIN `gallery_tags` ON `tags`.id = `gallery_tags`.tag_id WHERE ((`gallery_tags`.gallery_id = 34)) 

(它不是在查询中使用图片的gallery_id),然后我尝试添加

has_many :bookmarked_tags, :through => :bookmarked_images, :source => :tags 

到用户,这将导致User.find(1).bookmarked_tags导致

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_many :through for has_many :bookmarked_tags, :through => :bookmarked_images. Use :source to specify the source reflection.

所以:我怎样才能得到Rails的运行我贴有User.find查询(1)。 bookmarked_tags?

回答

3

有两种解决方案

首个解决方案

创建数据库内部的观点就像一个连接表:

CREATE VIEW user_bookmarks_tags (
    SELECT 
    bookmarks.user_id AS user_id 
    gallery_tags.tag_id AS tag_id 
    FROM 
    gallery_tags 
    INNER JOIN images ON gallery_tags.gallery_id = images.gallery_id 
    INNER JOIN bookmarks ON images.id = bookmarks.image_id 
    GROUP BY 
    user_id, tag_id 
) 

(你可以做到这一点迁移内) 这个视图就像一个“表”,其列user_id | tag_id

现在我们可以使用has_and_belongs_to_many适应我们的模型:

user.rb

class User < ActiveRecord::Base 
    has_many :bookmarks 
    has_many :galleries 

    # we use our magic (view) join table here! 
    has_and_belongs_to_many :tags, :join_table => :user_bookmarks_tags 
end 

tag.rb

class Tag < ActiveRecord::Base 
    # we use our magic (view) join table here 
    has_and_belongs_to_many :users, :join_table => :user_bookmarks_tags 
end 

现在你可以这样做:User.find(1).tagsTag.find(1).users :)


第二种解决

不要手动加入无一个观点:

定义缺失的关系(需要自动加入foreign_key查找):

标签。RB

class Tag < ActiveRecord::Base 
    has_many :gallery_tags 
end 

gallery_tag.rb

class GalleryTag < ActiveRecord::Base 
    belongs_to :gallery 
    belongs_to :tag 
    # new: 
    has_many :images, :through => :gallery 
end 

现在,我们可以添加一个bookmarked_tags方法我们user.rb

class User < ActiveRecordBase 
    has_many :bookmarks 
    has_many :galleries 

    def bookmarked_tags 
    Tag.joins(:gallery_tags => {:images => :bookmarks}).where('bookmarks.user_id = ?', self.id).group('tags.id') 
    end 
end 
+0

哇,有意思。我认为一种方法是必要的,但我从未使用过Arel的.joins。谢谢,这有效(我与第二个解决方案一起)。 –