1

レール3.0.3を使用しています。私は4つのテーブルを持っています。

class Artwork
  has_and_belongs_to_many :tag_styles
  has_and_belongs_to_many :tag_subjects
  has_and_belongs_to_many :tag_arttypes
end

class TagArttype
  set_table_name :tag_arttypes
  has_and_belongs_to_many :artworks
end

class TagStyle
  set_table_name :tag_styles
  has_and_belongs_to_many :artworks
end

class TagSubject
  set_table_name :tag_subjects
  has_and_belongs_to_many :artworks
end

これら3つのtag_tablesはすべてname属性を持っています。次のSQLを使用して、単語を受け入れ、「Abstract」の代わりに配置するArtworkスコープを作成するにはどうすればよいですか。

select a.* from artworks as a, tag_arttypes as ta, artworks_tag_arttypes as ata where a.id = ata.artwork_id and ta.id = ata.tag_arttype_id and ta.name = 'Abstract' union
 select a.* from artworks as a,tag_styles as ta,artworks_tag_styles as ata where a.id = ata.artwork_id and ta.id = ata.tag_style_id and ta.name = 'Abstract'union
 select a.* from artworks as a,tag_subjects  as ta,artworks_tag_subjects  as ata where a.id = ata.artwork_id and ta.id = ata.tag_subject_id and ta.name = 'Abstract';

ありがとう!

4

2 に答える 2

0

私があなたの質問を正しく理解していれば、それは非常に簡単です。以下のようなものが機能します:

scope :filter, lambda {|name| 
"
 (select a.* from artworks as a, tag_arttypes as ta, artworks_tag_arttypes as ata where a.id = ata.artwork_id and ta.id = ata.tag_arttype_id and ta.name = '#{name}') union
 (select a.* from artworks as a,tag_styles as ta,artworks_tag_styles as ata where a.id = ata.artwork_id and ta.id = ata.tag_style_id and ta.name = '#{name}')  union
 (select a.* from artworks as a,tag_subjects  as ta,artworks_tag_subjects  as ata where a.id = ata.artwork_id and ta.id = ata.tag_subject_id and ta.name = '#{name}')
"
}
于 2012-11-18T14:28:53.803 に答える