rails 中的 4 个模型可能存在 has_many 直通关系吗?
Is a has_many through relationship possible with 4 models in rails?
例如,一个列表和一个子列表可以通过名为 list_items 的模型包含多个项目。
这是模型
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
下面是我想要完成的。
项目图书馆页面
Item 1
Item 2
Item 3
Item 4
Item 5
列出页面
=============================
=List 1 =
=============================
=Item 2 =
=Item 4 =
=Sublist Start =
=Item 5 =
=Item 1 =
=Item 3 =
=============================
因此,没有子列表的项目(如项目 2 和项目 4)将在 List_Item 模型
中填充以下字段
List_id = 1
Sublist_id = nil
Item_id = 1
具有子列表的项目(如项目 5、项目 1 和项目 3)将在 List_Item 模型
中填充以下字段
List_id = 1
Sublist_id = 1
Item_id = 1
我想这样做的原因是我可以通过拖到子列表来进行拖放,它将填充 sublist_id 并拖出子列表 sublist_id然后将为零。
这可能吗,或者有更好的方法吗?
回答你的问题:是的,这是可能的:
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
如果 A 上的关联与 B 上的关联命名不同,您需要向 through
关系提供 source
参数,如下所示:
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
这将使您能够:
A.find(1).bs # => collection of B-instances
A.find(1).cs # => collection of C-instances
我不确定这是否能回答您的问题。我希望如此,但我对你的例子有点困惑,所以如果不是,请发表评论 :)
例如,一个列表和一个子列表可以通过名为 list_items 的模型包含多个项目。
这是模型
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
下面是我想要完成的。
项目图书馆页面
Item 1
Item 2
Item 3
Item 4
Item 5
列出页面
=============================
=List 1 =
=============================
=Item 2 =
=Item 4 =
=Sublist Start =
=Item 5 =
=Item 1 =
=Item 3 =
=============================
因此,没有子列表的项目(如项目 2 和项目 4)将在 List_Item 模型
中填充以下字段List_id = 1
Sublist_id = nil
Item_id = 1
具有子列表的项目(如项目 5、项目 1 和项目 3)将在 List_Item 模型
中填充以下字段List_id = 1
Sublist_id = 1
Item_id = 1
我想这样做的原因是我可以通过拖到子列表来进行拖放,它将填充 sublist_id 并拖出子列表 sublist_id然后将为零。
这可能吗,或者有更好的方法吗?
回答你的问题:是的,这是可能的:
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
如果 A 上的关联与 B 上的关联命名不同,您需要向 through
关系提供 source
参数,如下所示:
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
这将使您能够:
A.find(1).bs # => collection of B-instances
A.find(1).cs # => collection of C-instances
我不确定这是否能回答您的问题。我希望如此,但我对你的例子有点困惑,所以如果不是,请发表评论 :)