数据建模3路表的has_many协会 [英] Data Modeling 3 Way Table has_many association
问题描述
我试图建立一个表来处理的位置和一定的运动已被设置为与下面的模型协会类别:
I am attempting to build a table to handle both the location and category a certain campaign has been set to with the following model associations:
class Campaign < ActiveRecord::Base
has_many :campaign_category_metro_bids, dependent: :destroy
has_many :metros, through: :campaign_category_metro_bids
has_many :categories, through: :campaign_category_metro_bids
end
class Metro < ActiveRecord::Base
has_many :campaign_category_metro_bids
has_many :campaigns, through: :campaign_category_metro_bids
has_many :categories, through: :campaign_category_metro_bids
end
class Category < ActiveRecord::Base
has_many :campaign_category_metro_bids
has_many :campaigns, through: :campaign_category_metro_bids
has_many :metros, through: :campaign_category_metro_bids
end
class CampaignCategoryMetroBid < ActiveRecord::Base
belongs_to :campaign
belongs_to :category
belongs_to :metro
end
在试图创建一个竞选选择两个不同的城市和类别的结果是空的paramters之一作为ID:
When attempting to create a campaign for selecting two different cities and categories the result is NULL for the id of one of the paramters as:
广告制作code:
def new
if signed_in?
# create new campaign
@user = User.find(params[:id])
@campaign = @user.campaigns.new
else
redirect_to signin_path
end
end
def create
@campaign = User.find(params["campaign"]["user_id"]).campaigns.build(campaign_params)
if @campaign.save
flash[:success] = "Campaign created!"
redirect_to current_user
else
render 'new'
end
end
更新时间: 创建活动的视图使用两个单独的collection_select类别和地铁为:
UPDATED The view to create the campaign uses two separate collection_select for Category and Metro as:
<%= f.collection_select :category_ids, Category.all, :id, :display_category, {}, {multiple: true} %>
和
<%= f.collection_select :metro_ids, Metro.all, :id, :full_name, {}, {multiple: true} %>
campaigns_params:
campaigns_params:
def campaign_params
params.require(:campaign).permit(:name, :campaign_category_metro_bid_id,
:metro_ids => [], :category_ids => [])
end
有没有更好的方式来允许创建一个3台关系,因为我尝试?
还是有办法来链接类别
和新城
车型的选择,这样所产生的表像之下在广告制作
Is there a better way to allow for the creation of a 3 table relation as I am attempting?
or a way to link the Category
and Metro
models at selection so that the resultant table is something like below upon campaign creation:
推荐答案
我想问题可能是多选择,你有类别和地铁。你基本上试图将多个foreign_keys同一参考成单行记录。如果类别ID和地铁ID都被定义为整数,你需要以能够保存这个创造多个记录。
I think the problem may be the multi select that you have on categories and metros. You're essentially trying to fit multiple foreign_keys for the same reference into a single row record. If category ID and metro ID are both defined as integers, you would need to create multiple records in order to be able to save this.
您将需要添加一些逻辑,看看您的选择PARAMS有> 1的长度,并根据您需要创建和保存一个新行。逻辑会是这个样子
You would need to add some logic to see if your selection params have a length of > 1 and based on that you'll need to create and save a new row. The logic would look something like this
params[:category_ids].each do |category|
params[:metro_ids].each do |metro|
@user.campaign.create(category_id: category, metro_id:metro) #any other params would go here too
end
end
这通过您的多选将基本上循环来为每个组合的新纪录。
This would essentially loop through your multi-select to create a new record for each combination.
这篇关于数据建模3路表的has_many协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!