3

Given Problem & Observer model:

class Problem < ActiveRecord::Base
  has_many :observers
  accepts_nested_attributes_for :observers
end

class Observer < ActiveRecord::Base
  belongs_to :problem
  belongs_to :user
end

I am trying to use form_for to select users as observers:

        <%= f.fields_for :observers do |obs| %>
            <%= obs.collection_select(:user_id, Users.to_a, :id, :name, {:include_blank => true, include_hidden: false}, {:multiple => true}) %>
        <% end %> 

However Rails generates wrong name for select: problem[observers_attributes][0][user_id][], so even creating a rule for strong_params ({:observers_attributes => [{:user_id => []}]}) it creates wrong relationship, only problem_id goes to the database, and all user_id are being ignored.

What I'm trying to do is to show all users in multiple select, grab IDs and create association for them in Problem#new method.

UPDATE 12.10

Posted Parameters:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"NHDl/hrrFgATQOoz9A3OLbLDAbTMziKMQW9X1y2E8Ek=", "problem"=>{"problem_data_attributes"=>{"title"=>"safasfasfafsasf", "description"=>""}, "observers_attributes"=>{"0"=>{"user_id"=>["5", "8"]}}}}

Strong Params:

def problem_params
 params.require(:problem).permit({:files_attributes => [:attach_id]}, {:observers_attributes => {:user_id => []}}, {:problem_data_attributes => [:title, :description]})
end

Create method

def create
 @problem         = @project.problem.build(problem_params)
 @problem.account = current_account

 if @problem.save
  render :json => {status: true, id: @problem.id}
 else
  respond_with(@problem)
 end
end

And SQL that creates observer during create call:

SQL (0.2ms)  INSERT INTO `observers` (`problem_id`) VALUES (96)
nateless
  • 475
  • 6
  • 23
  • What you do should work. What rails generate for the select menu is also correct. Have you tried to see what's inside your params hash? And also, have do you save this, what does your controller look like? – jokklan Oct 07 '13 at 13:18
  • @jokklan yeap, I'm receiving `"observers_attributes" => { "0" => { "user_id" => [ [0] "5" ] } }` as a hash and rails generates SQL (0.2ms) `INSERT INTO observers (problem_id) VALUES (58)`, where 58 is a problem id, however user_id is being ignored. – nateless Oct 08 '13 at 16:41
  • There must also be a `"problem_id" => "58"` in your params then? And what exactly is your permit and require options for strong params? Could you maybe post your whole controller action? – jokklan Oct 10 '13 at 09:04
  • @jokklan, No, problem_id is being created by rails. I've added code and logs to the question. – nateless Oct 12 '13 at 15:28

1 Answers1

5

In the way that you are doing it, you are saying that you only want an observer with multiple user_ids, and in fact what you want is one observer per user (and problem).

You should probably use an association model like this: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

Please remember to create the association in alphabetic order, in your case model ProblemUser and table problems_users.

Then, you can do the form like in here: https://stackoverflow.com/a/9917006/1217298 – please read the question and the answer to better understanding.

Hope it helps.

Community
  • 1
  • 1
Gustavo Semião-Lobo
  • 2,468
  • 3
  • 18
  • 26
  • It helped, I switched to has_many through because of additional ACL validations but thanks again for pointing me to habtm relation. – nateless Oct 12 '13 at 16:11