Hi,
I have this requirement where in the association needs to be between 3 models.
I want a User to select a Gift to be sent to a particular Destinations. For this I created a Model RequestForGift with foreign keys ( belongs_to ) to the User, Gift and Destination.Then I added the has_many relationships to each of the 3 Models to the other 2, but some how the usage of the association through the models is not comforting
class User < ActiveRecord::Base has_many :RequestForGifts has_many :Gifts, :through => :RequestForGifts, :uniq => true has_many :Destinations, :through => :RequestForGifts, :uniq => true end
class Gift < ActiveRecord::Base has_many :RequestForGifts has_many :Users, :through => :RequestForGifts, :uniq => true has_many :Destinations, :through => :RequestForGifts, :uniq => true end
class Destination < ActiveRecord::Base has_many :RequestForGifts has_many :Users, :through => :RequestForGifts, :uniq => true has_many :Destinations, :through => :RequestForGifts, :uniq => true end
class RequestForGift < ActiveRecord::Base belongs_to :Gift belongs_to :User belongs_to :Destination end
i.e. I am not able to find All the Gifts sent by a User to New Delhi, India
Basically i am looking for some code like 1) User.find(params[:id]).gifts.select( |g| { g.destination.name = 'New Delhi, India' } )
I know that the same can be achieved by using the RequestForGift directly :-
a) destination_id = Destination.find_by_name ( :first, 'New Delhi, India' ) RequestForGift.find ( :all, :conditions => [ 'destination_id = ? and user_id = ?', destination_id, params[:id] ] ).collect ( |r| r.gift ) or b) User.find(params[:id]).requestForGifts.select ( |rfg| rfg.destination.name = 'NewDelhi, India' ).collect ( |rfg| rfg.gift )
But using query a or b would mean that the :has_many :through requirements that I have written will be of no use, since they are only double associative - The association assumes that only 2 models are involved in the association.
I am facing such models at a really high frequency, and a code fragment like query 1, I believe would really mprove understandability, of the code since the association table is never involved in the ruby code.
Can someone help me out with the model, or if it is even possible with the current associations that RoR supports?
Thanks, Abhin H