Reputation: 806
I can't comprehend how I would scope through a child using a model that isn't fully associated with it. The main model being Kid and the other being Favorite. In the app, a user can have Favorite moms and a Kid belongs to a mom and a dad. Mom and Dad don't belong to anything:
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :mom
end
class Kid < ActiveRecord::Base
belongs_to :user
belongs_to :dad
belongs_to :mom
end
class User < ActiveRecord::Base
has_many :kids
has_many :favorites
end
As you can see, a Favorite doesn't have any association to Kid. I'm guessing it would if I made it go through each other like favorite.kids or kid.favorites but the latter doesn't make sense to me because of what I want to do with it.
What I want to do is create a scope that list a dads' kids but only for the ones that are a user's favorite moms. So something like:
@dad = Dad.find(params[:dad_id])
@kids = @dad.kids.current_users_favorite_moms
But which model what I start on Favorites? Dad? Mom? I'm not sure. Could you help me?
Upvotes: 0
Views: 27
Reputation: 42799
First of all I don't understand the relation between the Kid
and the User
so I'm gonna drop it.
I think it would be easier if you used has_many :through
class User < ActiveRecord::Base
has_many :favorite_moms, through: :favorites, class_name: :mom
end
Class Kid < ActiveRecord::Base
belongs_to :mom
belongs_to :dad
end
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :mom
end
Then we'd do this
@dad = Dad.find(id)
@kids = Kid.where(dad: @dad, mom: current_user.favorite_moms)
Try it and tell me if it works.
Upvotes: 2