Rails Multi Level has_many through

时间:2014-09-26 00:29:07

标签: ruby-on-rails activerecord database-design

我正在尝试构建如所示的多级模型。当用户创建活动时,他们将从表类别和地铁中选择一些类别和一些地铁。然后,这些选择将作为campaign_category和campaign_metro放在两个表格中。但是,在创建这些条目后,我希望在表campaign_category_metro中填充这些组合的另一个表,以允许链接为用户选择的广告系列,类别和地铁以设置出价。

更新:

例如,假设用户创建一个ID为10的广告系列,并选择ID为1和2的类别,以及ID为3和4的地铁。在表格中显示campaign_category:

enter image description here

并在表campaign_metro中显示:

enter image description here

在campagin_category_metro中,我希望将上表中的那些置换行作为:

enter image description here

然后,用户可以针对这些广告系列/地铁组合设置出价。

截至目前,在创建广告系列以及选择类别和地铁时,我可以填充表campaign_category和campaign_metro,但最终表campaign_category_metro仍为空白。以下是我的模型:

第一级:

class Campaign < ActiveRecord::Base   
    has_many :campaign_metros, dependent: :destroy
    has_many :metros, through: :campaign_metros

    has_many :campaign_categories, dependent: :destroy
    has_many :categories, through: :campaign_categories
end

class Category < ActiveRecord::Base
    has_many :campaign_categories, dependent: :destroy
    has_many :campaigns, through: :campaign_categories
end

class Metro < ActiveRecord::Base   
    has_many :campaign_metros, dependent: :destroy
    has_many :campaigns, through: :campaign_metros
end

第二级(填充,但与campaign_category_metro的关系不执行任何操作):

class CampaignCategory < ActiveRecord::Base
    belongs_to :campaign
    belongs_to :category

    has_many :campaign_category_metros, dependent: :destroy
    has_many :campaign_metros, through: :campaign_category_metros
end

class CampaignMetro < ActiveRecord::Base
    belongs_to :campaign
    belongs_to :metro

    has_many :campaign_category_metros, dependent: :destroy
    has_many :campaign_categories, through: :campaign_category_metros
end

第三级(创建广告系列时不显示任何内容):

class CampaignCategoryMetro < ActiveRecord::Base
    belongs_to :campaign_category
    belongs_to :campaign_metro
end

如何在制作广告系列时允许使用所选类别和地铁的总排列量填充第三级表格?

1 个答案:

答案 0 :(得分:1)

这里有一些代码会导致您在示例中发布的campaign_category_metro表:

campaign = Campaign.create # pretend the id is now 10

# this will create the join tables you provided as examples
campaign.categories << [Category.find(1), Category.find(2)]
campaign.metros << [Metro.find(3), Metro.find(4)]

# Get the join tables
campaign_categories = campaign.campaign_categories
campaign_metros = campaign.campaign_metros

我们现在有了这些:

campaign_categories campaign_metros

到目前为止一直很好,只需设置一些上下文以确保我们在同一页面上。现在生成最终排列并创建最后一个表:

@campaign_category_metros = []

# this outer loop will run twice and provide campaign_category ids 15 and 16
campaign_categories.each do |campaign_category|
  # this inner loop will run twice and provide campaign_metro ids 25 and 26
  campaign_metros.each do |campaign_metro|
    # this next bit will run 4 times and pair 15 with 25 and 26 and 16 with 25 and 26
    @campaign_category_metros << CampaignCategoryMetro.create({
      campaign_category_id: campaign_category.id,
      campaign_metro_id: campaign_metro.id
    })
  end
end

现在@campaign_category_metros将等于:

campaign_category_metro

当然除了空白出价。您可以在列表中呈现@campaign_category_metros,以允许用户对这4种组合进行出价。

这里的想法是,对于每个campaign_category,我们将为每个campaign_category_metro创建一个campaign_metro记录。

注意: Rails中没有构建机制可以为您完成此任务。