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

北战南征 提交于 2019-12-19 12:31:43

问题


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?


回答1:


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 :)



来源:https://stackoverflow.com/questions/31358679/is-a-has-many-through-relationship-possible-with-4-models-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!