samedi 20 février 2016

Rails joins query with has_many through

I am trying to make a search function on my rails app with the following models and relationships

    class Favor < ActiveRecord::Base

    has_many :compensations
    has_many :rewards, through: :compensations

    end

    class Compensation < ActiveRecord::Base
      belongs_to :favor
      belongs_to :reward
    end

    class Reward < ActiveRecord::Base
      has_many :compensations
      has_many :favors, through: :compensations

      has_many :rewards_searches
      has_many :searches, through: :rewards_searches
    end

    class Search < ActiveRecord::Base

      has_many :rewards_searches
      has_many :rewards, through: :rewards_searches
    end

    class RewardsSearch < ActiveRecord::Base
      belongs_to :reward
      belongs_to :search
    end

What I need is a joins or includes query that says: All the Favor objects that have any of the rewards in a specific search.rewards object.

Thanks a lot for your time in advance!

Aucun commentaire:

Enregistrer un commentaire