タグ

conversionとwhereに関するkiyo_hikoのブックマーク (1)

  • ruby rails activerecord nested select

    I have three tables/models. Recordings (id) has_many :hits has_many :tags, :through => :hits Hits (id, recording_id, tag_id) belongs_to :recordings belongs_to :tags Tags (id) has_many :hits has_many :recordings, :through => :hits I need to find all recordings that have all the tags that are passed in as a parameter. For example, find all recordings that have tag.id == 5, tag.id == 6, and tag.id ==

    ruby rails activerecord nested select
    kiyo_hiko
    kiyo_hiko 2015/12/07
    "SELECT * FROM recordings WHERE id IN (SELECT recording_id FROM hits WHERE recording_id IN (SELECT recording_id FROM hits WHERE recording_id IN (SELECT recording_id from hits WHERE recording_id = 5)AND tag_id = '6') AND tag_id == '7')" → "Recording.joins(:tags).where(:tags => { :id => [5,6,7] })"
  • 1