Search code examples
ruby-on-railsruby-on-rails-4relationships

Is a has_many through relationship possible with 4 models in rails?


For example a list and a sublist can have many items through a model called list_items.

Here are the models

class List < ActiveRecord::Base
    has_many :sublists
    has_many :list_items
    has_many :items, through: :list_items
end

class Sublist < ActiveRecord::Base
    belongs_to :list
    has_many :list_items
    has_many :items, through: :list_items
end

class Item < ActiveRecord::Base
    has_many :list_items
    has_many :lists,through: :list_items
    has_many :sublists, through: :list_items
end

class ListItem < ActiveRecord::Base
  belongs_to :list
  belongs_to :sublist
  belongs_to :item
end

This below is what i am trying to accomplish.

Items Library Page

Item 1
Item 2
Item 3
Item 4
Item 5 

Lists Page

=============================
=List 1                     =
=============================
=Item 2                     =
=Item 4                     =
=Sublist Start              =
=Item 5                     =
=Item 1                     =
=Item 3                     =
=============================

So an item without a sublist (like item 2 and item 4) would have the following fields populated in the List_Item Model

List_id = 1

Sublist_id = nil

Item_id = 1

An item with a sublist (like item 5 and item 1 and item 3) would have the following fields populated in the List_Item Model

List_id = 1

Sublist_id = 1

Item_id = 1

The reason i would like to do it this way is so i can do drag and drop by dragging to the sublist it will populate the sublist_id and by dragging out of the sublist the sublist_id will then be nil.

Would this be possible or is there a better way to do this this?


Solution

  • To answer your question: Yes, that's possible:

    class A
      has_many :bs
      has_many :cs, through: :bs
      has_many :ds, through: :cs
    end
    
    class B
      has_many :cs
      belongs_to :a
    end
    
    class C
      has_many :ds
      belongs_to :b
    end
    
    class D
      belongs_to :c
    end
    

    If th association on A is named differently than the association on B, you need to supply the source parameter to the through-relation, like this:

    class A
      has_many :bs
      has_many :cs, through: :bs, source: :ccs
    end
    
    class B
      has_many :cs, as: :ccs
      belongs_to :a
    end
    
    class C
      belongs_to :b
    end
    

    This will allow you to:

    A.find(1).bs # => collection of B-instances
    A.find(1).cs # => collection of C-instances
    

    I'm not sure, if this answers your question. I hope so, but I'm a little confused about your example, so if not, please do comment :)