简体   繁体   English

包含模型,必须属于其他2个模型

[英]model include, must belong to 2 other models

This is really bugging me - cannot seem to get the associations set up correct with includes and so fort. 这确实让我烦恼-似乎无法通过包含等建立正确的关联。 Here are the models in question: 这是有问题的模型:

class Category < ActiveRecord::Base
 has_many :subcategories
 has_many :locations, :through => :subcategories
end
class Location < ActiveRecord::Base
 has_many :subcategories
 has_many :category, :through => :subcategories
end
class Subcategory < ActiveRecord::Base
 belongs_to :category
 belongs_to :location
end

I need to output in following structure: 我需要以以下结构输出:

Category.name
 Location.name
  Subcategory.name
  Subcategory.name
  Subcategory.name
       (subcategory from another sub category)

Controller: 控制器:

 class SubcategoriesController < ApplicationController
    def index
      @categories = Category.all(:include => [:locations => :subcategories], :group
      =>"subcategories.name")
    end

View: 视图:

 <div class="categorylist">
  <ul>
     <%= @categories.each do |category|%>
    <h3>
        <%=h link_to category.name, category %>
    </h3>
    <%= category.locations.each do |location|%>
    <h6>
        <%= link_to location.name, location %>
    </h6>
        <%= location.subcategories.each do |subcategory|%>
        <p>
        <%= link_to subcategory.name, subcategory%>
        </p>
        <% end %>   
    <% end %>
   <% end %>    
  </ul> 
 </div>

what I want is: 我想要的是:

 Fruit
  France
    apple
      granny smith
    fig
  India
    pineapple
    banana
 Meat
   India 
     cow
     chicken
     -------------
What I am currently getting:
Fruit
  India
    cow 
    chicken
    banana
    pineapple

Meaning I am getting the subcategories based solely on the location and not on location and category.. 这意味着我仅根据位置而不是位置和类别来获取子类别。

My problem arises, when i reach the subcategories level, where subcategories should depend on both category and location - They only ever seem to belong to only 1 of the other models, but in my case they must belong to both. 当我到达子类别级别时,出现了我的问题,其中子类别应同时取决于类别和位置-它们似乎只属于其他模型中的一种,但是在我的情况下,它们必须属于两者。 I can't seem to get this right. 我似乎无法正确理解。 I have tried numerous ways to do this and not sure im even going about it correctly. 我尝试了很多方法来做到这一点,甚至不确定即时消息是否正确。

Hope someone can point me in the right direction. 希望有人能指出正确的方向。

Thanks in advance Christian 在此先感谢克里斯蒂安

If I understand your problem correctly, you have something like 如果我正确理解您的问题,您会遇到类似

#Category
Fruit
 Apple
 Banana
Vegetable
 Lettuce

#Location
France
 Apple
 Lettuce
India
 Banana

Now, you want something like 现在,您想要类似

#Hierachical output
Fruit
 France
  Apple
 Vegetable
  Lettuce
India
 Fruit
  Banana

In that case, wouldn't this work => location.subcategories.where(category_id: category.id).each ? 在那种情况下,这样行不通=> location.subcategories.where(category_id: category.id).each吗?

Edit : 编辑

Perhaps, this could work (in addition to above change): 也许,这可能有效(除了上述更改之外):

<%= category.locations.all(include: sub_categories).each do |location|%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM