Data modeling 3-room association has_many

I am trying to create a table to handle both the location and the category in which a particular campaign was installed 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 

When trying to create a campaign to select two different cities and categories, the result is NULL for the identifier of one of the parameters as:

enter image description here

Campaign Creation 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 

UPDATED The view for creating the campaign uses two separate collection_select objects for the category and Metro as:

  <%= f.collection_select :category_ids, Category.all, :id, :display_category, {}, {multiple: true} %> 

and

  <%= f.collection_select :metro_ids, Metro.all, :id, :full_name, {}, {multiple: true} %> 

campaigns_params:

  def campaign_params params.require(:campaign).permit(:name, :campaign_category_metro_bid_id, :metro_ids => [], :category_ids => []) end 

Is there a better way to allow the creation of a relation of 3 tables as I try? or a way to bind Category and Metro models when choosing so that the resulting table looks like this when creating a campaign:

enter image description here

+5
source share
1 answer

I think the problem may lie in choosing a few items that you have by category and metro. You are trying to put multiple foreign_keys for the same link in a single line entry. If the category identifiers and the metro identifier are defined as integers, you will need to create several entries to save this.

You will need to add some logic to see if your selection options have a length> 1 and based on this you will need to create and save a new line. The logic will 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 will essentially go through your multi-select to create a new record for each combination.

0
source

Source: https://habr.com/ru/post/1203345/


All Articles