0

I have 3 models, Parent, Children and GrandChildren

Class Parent < ActiveRecord::Base
  has_many: childrens
end

Class Children < ActiveRecord::Base
  belongs_to: parent  
  has_many: grand_childrens
end

Class GrandChildren < ActiveRecord::Base
  belongs_to: children
  belongs_to: user
  validates :user_id, presence: true
end

Each users can create infinite amount of grandchildren, but they can only create one grandchildren for each children.

In my view, I am doing this :

@parents.each do |parent|
  parent.childrens.each do |children|
    if children.grand_childrens.where("user_id = (?)",current_user.id).blank? 
      // tells user he has not created grand children for this children 
    else
      //tells user he has created a grand children for this children
    end
  end
end

The code above caused my app to do a query everytime i checked whether the children's grand childrens are blank. I searched a bit and found out that I can solve this by using includes.

I tried this :

Parent.includes([childrens: :grand_childrens]).references(:grand_childrens).where("grand_childrens.user_id = (?)",current_user.id)

but it will return only childrens who have grand childrens created by the current user. If a children doesn't have a grand children created by the current user, it won't be included.

I want to get all childrens whether they have grandchildrens created by the current user or not.

I searched around and found this question : Rails includes with conditions. the solution proposed was to create scopes with conditions like this :

 has_many   :current_user_grand_childrens, :class_name => "GrandChildren", 
               :conditions  => { user_id: current_user.id }

But this would require me to include SessionHelper in models and I would like to avoid that.

Any help is greatly appreciated.

Community
  • 1
  • 1
Stefan W.
  • 13
  • 1
  • 5

1 Answers1

0

If you are ok with an array being returned(it has to be an array, since Parent, Children and Grandchildren are different models) then this should work:

def all_entities_created_by_user user
  Parent.all + 
  Children.all + 
  GrandChildren.where(user_id: user.id)
end

Update 1: edited code above to replace where with all in Parent and Children, based on comment below.

Anand
  • 3,690
  • 4
  • 33
  • 64
  • The model that is created by the user is only the grandchildren, the children and parent has no direct relation with the user. I want to take all the parents and the childrens, but i want to apply the condition grand_childrens.user_id = current_user.id only on the grand children – Stefan W. Sep 03 '15 at 14:43
  • @StefanWijayaD. Edited the solution based on your comment – Anand Sep 03 '15 at 15:37
  • Is it possible to get this array back to activerecord object? I wanted to access the objects like this : parent.childrens – Stefan W. Sep 03 '15 at 16:20
  • You can always do `parent.childrens` on a `parent` object, or `child.grandchildrens` to get all the children/grandchildren respectively. Also, the array contains ActiveRecord objects for each object. But you cannot get a single ActiveRecord::Relation with three different types of ActiveRecord objects (Parent, Children and Grandchildren). – Anand Sep 03 '15 at 18:27