问题基于单个过滤属性通过连接模型访问数据

时间:2011-09-08 06:46:19

标签: ruby-on-rails ruby activerecord data-modeling has-many-through

我正在模仿歌曲和艺术家之间的关系:

class Song < ActiveRecord::Base
  # Relationships 
  has_many  :releases
  has_many  :artists, :through => :releases
  has_many  :featured_artists,  :through => :releases,
                                :class_name => "Artist",
                                :source => :artist,
                                :conditions => { :releases => { :featured => true } }
end


class Artist < ActiveRecord::Base
  # Relationships
  has_many  :releases
  has_many  :songs, :through => :releases
  has_many  :featured_songs,  :through => :releases,
                              :class_name => "Song",
                              :source => :song,
                              :conditions => { :releases => { :featured => true } }
end


class Release < ActiveRecord::Base
  # Relationships
  belongs_to :artist
  belongs_to :song

  # Class methods
  def self.featured?
    self.featured == true
  end
end

这成功地支持了一首歌可以拥有许多艺术家的概念;其中一些是歌曲中的“特色”。 (想像说唱歌曲)

问题在于我不知道如何访问拥有以下内容的ONE艺术家:featured =&gt;假(我认为这是这首歌的“主要艺术家”)。

我喜欢的是在我的Song模型中添加与此类似的东西的一些方法:

  has_one  :artist,  :through => :releases,
                                  :class_name => "Artist",
                                  :source => :artist,
                                  :conditions => { :releases => { :featured => false } }

这显然不起作用,但似乎是正确的想法 - 我能够根据发布连接模型设置/获取该艺术家:features =&gt;假的。

有什么想法吗?

谢谢!

0 个答案:

没有答案