0

Can't save params selected on select box.

Table users:

1id| |name|
1     CR7
2     Messi

Table ejecutives:

1id| |name|
1     Mourinho
2     Guardiola

Table user_ejecutives:

|id| |user_id|  |ejecutive_id|
1     1          1    
2     2          2

Controller users_controller.rb:

def new
  @obj_user = User.new
end

def create
  @user = User.new user_params
  @user.save
end

def show
  @user = User.find(params[:id])
end

private
 def user_params
   params.require(:user).permit(:name, user_ejecutive_ids: [])
 end

Models:

#User.rb
has_many :ejecutives, :through => :user_ejecutives
has_many :user_ejecutives
has_and_belongs_to_many :user_ejecutives, class_name: "User", join_table: "user_ejecutives"#, foreign_key: :user_id, association_foreign_key: :ejecutive_id  

#Ejecutive.rb
has_many :user_ejecutives
has_many :users, :through => :user_ejecutives

#UserEjecutive.rb
belongs_to :user
belongs_to :ejecutive

View new.html.erb:

<%= form_for @user do |f| %>
   <%= form.text_field :name %>
   <%= f.collection_select :user_ejecutive_ids, Ejecutive.all, :id, :name, multiple: true %>
<% end %>

View show.html.erb

<% @user.ejecutives.each do |ejecutive| %>
  <%= ejecutive.name %></label>
<% end %>

I'm not getting results on the view show and it show on logs:

SystemStackError (stack level too deep):
Carlos Morales
  • 1,137
  • 3
  • 15
  • 38

2 Answers2

1

Try the following code.

params.require(:user).permit(:name, :user_ejecutives => [])

Hey, I think you have "ejecutive_id" column declared as integer but when loop through "user_ejecutives" you are getting each value as string, May be this is causing the issue, Kindly update your create action to below.

  def create
    obj_user = User.new(user_params)

    if obj_user.save
      params[:user_ejecutives].each do |ejecutive|
        user_ejecutive = UserEjecutive.create(user_id: obj_user.id, ejecutive_id: ejecutive.to_i)
        user_ejecutive.save
      end
    end
  end
Dheeresha
  • 797
  • 1
  • 4
  • 6
1

If you're just trying to populate the join table (user_ejecutives), you'll want to populate the singular_colletion_ids method:

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def new
     @user = User.new
   end

   def create
     @user = User.new user_params
     @user.save
   end

   private

   def user_params
     params.require(:user).permit(:name, user_ejecutive_ids: [])
   end
end

#app/views/users/new.html.erb
<%= form_for @user do |f| %>
   <%= f.collection_select :user_ejecutive_ids, User.all, :id, :name, multiple: true %>
   <%= f.submit %>
<% end %>

This will assign new user_ejecutives for each new @user you create.

PS User.all is valid in this instance as you're dealing with a new (uncreated) @user record, hence it won't appear in the db.


If you wanted to create new user_ejecutives with each new @user, you'll want to use accepts_nested_attributes_for, which I can explain if required.


Update

So your error is as follows:

Unpermitted parameter: user_ejecutive_ids

... you also have another error...

NoMethodError (undefined method `each' for nil:NilClass):

This is exactly why I don't like your code. Because it doesn't fit to convention, you've go to evaluate whether the params are present etc.

You'll need to use the controller code I posted - it will populate the other table for you, and fix this NilClass error.

--

Join Table

Your user_ejecutives table is a join table.

Your User model should have the following:

#app/models/user.rb
class User < ActiveRecord::Base
   has_and_belongs_to_many :user_ejecutives, class_name: "User", join_table: "user_ejecutives", foreign_key: :user_id, association_foreign_key: :ejecutive_id
end

You'll have to remove the id column from your user_ejecutives table (as per the definition here). The importance of this is that it gives you the ability to populate the singular_collection_ids method (in your case user_ejective_ids), as per my recommended code.

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • It's not (it opens the door to so potential sql injection). If you post your request log, I'll give you the correct definition – Richard Peck Feb 02 '16 at 15:06
  • Have you tested my code? I really don't like yours, it's too cumbersome – Richard Peck Feb 02 '16 at 15:25
  • Let's check it out!! – Richard Peck Feb 02 '16 at 15:32
  • Up to you, I can only recommend what I'd do and what convention is – Richard Peck Feb 02 '16 at 16:07
  • You're at liberty to do that, I'm just saying that you have a join table and that it's best to use the recommendation I have outlined. I even linked to another website which shares a similar strategy. If you want to do it your way, you'll have to fix the flow to accommodate any errors you get – Richard Peck Feb 02 '16 at 16:11
  • [Yep](http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many) `The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity.` -- if you want to use `id`, just make a model for your join table and use `has_many :through` (I can update if required). HABTM - I believe - doesn't work with `id` in the table though – Richard Peck Feb 09 '16 at 17:08
  • `stack level too deep` basically means infinite recursion -- do you have time to jump into chat? – Richard Peck Feb 09 '16 at 21:40
  • Richard i found the error and is still SystemStackError (stack level too deep): – Carlos Morales Feb 10 '16 at 16:38
  • When are you available to chat again? – Richard Peck Feb 10 '16 at 20:57