I have the models Dad, Mom and Kid. I have a boolean attribute on my Mom called is_online
. Now I'm doing a pagination chain like this:
def show
@dad = Dad.find(params[:dad_id])
@kids = @dad.kids.online.paginate(:page => params[:page])
end
Allowing me to see a list of the Dad's kids who's moms are online only.
My models are setup like this:
class Kid < ActiveRecord::Base
belongs_to :dad
belongs_to :mom
scope :online, joins(:mom).where("moms.is_online = ?", true)
end
class Dad < ActiveRecord::Base
has_many :kids
end
class Mom < ActiveRecord::Base
has_many :kids
end
The problem I'm having is this doesn't return just this Dad's kids with online moms but instead returns ALL Dad's. How can I get the scope to be correct?
EDIT
SELECT DISTINCT "kids"."dad_id" FROM "kids"
=> [14, 25, 27, 8, 12, 17, 28, 1, 15, 10, 26, 11, 4, 18, 30, 16, 6, 19, 29, 2, 21, 3, 23, 31, 20, 5, 13, 22, 9, 7, 24]
Kid Load (1.5ms) SELECT "kids".* FROM "kids" INNER JOIN "moms" ON "moms"."id" = "kids"."mom_id" WHERE (moms.is_online = 't')
=> #<ActiveRecord::Relation [
#<Kid id: 7, dad_id: 1, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 21, dad_id: 2, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 35, dad_id: 3, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 49, dad_id: 4, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 63, dad_id: 5, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 77, dad_id: 6, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 91, dad_id: 7, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 105, dad_id: 8, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 119, dad_id: 9, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">,
#<Kid id: 133, dad_id: 10, mom_id: 5, created_at: "2014-04-28 16:10:48", updated_at: "2014-04-28 16:10:48">, ...]>