Rails 4.0 has_many_through and fields_for

Hi all, I am trying to reproduce rails 3.2 behaviour with fields_for and nested attributes.

class ControllerAction < ActiveRecord::Base
  has_many :interactions, dependent: :destroy
  has_many :roles, through: :interactions

  scope :controllers, lambda {|name| where("controller_name_id = ?", name)}
  scope :actions, lambda {|name| where("action_name_id = ?", name)}

  def initialized_interactions() # this is the key method
    [].tap do |o|
      Role.all.each do |r|
        if p = interactions.find { |p| p.role_id == r.id }
          o << p.tap { |p| p.enable ||= true }
        else
          o << Interaction.new(:role_id => r.id)
        end
      end
    end
  end
end

class Role < ActiveRecord::Base
  has_many :interactions
end

class Interaction < ActiveRecord::Base
  belongs_to :controller_action
  belongs_to :role
  attr_accessor :enable # nice little thingy here
  # It is now recommended to use strong params instead of   'attr_accessible :enable, :controller_action_id, :role_id'
end

controller_actions_controller.rb

private
# Use callbacks to share common setup or constraints between actions.
def set_controller_action
@controller_action = ControllerAction.find(params[:id])
end

    # Never trust parameters from the scary internet, only allow the white list through.
    def controller_action_params
      params.require(:controller_action).permit!
    end

controller_actions view - excluding error code and most css formatting.
_form.rb

<%= form_for(@controller_action) do |f| %>
  ...
  <%= f.label :name %><br />
  <%= f.text_field :name %>
 
  <h2><%= f.label :role %></h2>
  <%= f.fields_for :interactions, @controller_action.initialized_interactions() do |builder| %>
      <% role = builder.object.role %>
      <%= builder.hidden_field :role_id %>

      <%= builder.check_box :enable %>
      <%= builder.label :enable, role.name %>

  <% end %>

  <%= f.submit %>

<% end %>

Under rails 3.2.11 I get a form that allows the user to edit roles.

Editing controller_action

Show |
Back

Under Rails 4.0 you get:-

undefined method `role' for #<Array:0xb875d4c>

Extracted source (around line #21):

18
19
20
21
22
23
24
<div>

<h2><%= f.label :role %></h2>

<%= f.fields_for :interactions, @controller_action.initialized_interactions() do |builder| %>

<% role = builder.object.role %>

<%= builder.hidden_field :role_id %>

<div class="field">

Trace of template inclusion: app/views/controller_actions/edit.html.erb

`Rails.root: /home/john/RubyMineProjects/Rails/4.0/urps2`

[Application Trace](http://localhost:3100/controller_actions/1/edit#) |
[Framework Trace](http://localhost:3100/controller_actions/1/edit#) |
[Full Trace](http://localhost:3100/controller_actions/1/edit#)

app/views/controller_actions/_form.html.erb:21:in block (2 levels) in _app_views_controller_actions__form_html_erb__366592963_95670400' actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:38:in block in capture’
actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:200:in with_output_buffer' actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:38:in capture’
actionpack (4.0.0.beta1) lib/action_view/helpers/form_helper.rb:707:in fields_for' actionpack (4.0.0.beta1) lib/action_view/helpers/form_helper.rb:1501:in fields_for’
app/views/controller_actions/_form.html.erb:20:in block in _app_views_controller_actions__form_html_erb__366592963_95670400' actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:38:in block in capture’
actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:200:in with_output_buffer' actionpack (4.0.0.beta1) lib/action_view/helpers/capture_helper.rb:38:in capture’
actionpack (4.0.0.beta1) lib/action_view/helpers/form_helper.rb:435:in form_for' app/views/controller_actions/_form.html.erb:1:in _app_views_controller_actions__form_html_erb__366592963_95670400’
actionpack (4.0.0.beta1) lib/action_view/template.rb:143:in block in render' activesupport (4.0.0.beta1) lib/active_support/notifications.rb:160:in instrument’
actionpack (4.0.0.beta1) lib/action_view/template.rb:141:in render' actionpack (4.0.0.beta1) lib/action_view/renderer/partial_renderer.rb:306:in render_partial’
actionpack (4.0.0.beta1) lib/action_view/renderer/partial_renderer.rb:279:in block in render' actionpack (4.0.0.beta1) lib/action_view/renderer/abstract_renderer.rb:23:in block in instrument’
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in block in instrument' activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in instrument’
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in instrument' actionpack (4.0.0.beta1) lib/action_view/renderer/abstract_renderer.rb:23:in instrument’
actionpack (4.0.0.beta1) lib/action_view/renderer/partial_renderer.rb:278:in render' actionpack (4.0.0.beta1) lib/action_view/renderer/renderer.rb:41:in render_partial’
actionpack (4.0.0.beta1) lib/action_view/helpers/rendering_helper.rb:27:in render' app/views/controller_actions/edit.html.erb:3:in _app_views_controller_actions_edit_html_erb__785330494_92185850’
actionpack (4.0.0.beta1) lib/action_view/template.rb:143:in block in render' activesupport (4.0.0.beta1) lib/active_support/notifications.rb:160:in instrument’
actionpack (4.0.0.beta1) lib/action_view/template.rb:141:in render' actionpack (4.0.0.beta1) lib/action_view/renderer/template_renderer.rb:49:in block (2 levels) in render_template’
actionpack (4.0.0.beta1) lib/action_view/renderer/abstract_renderer.rb:23:in block in instrument' activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in block in instrument’
activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in instrument' activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in instrument’
actionpack (4.0.0.beta1) lib/action_view/renderer/abstract_renderer.rb:23:in instrument' actionpack (4.0.0.beta1) lib/action_view/renderer/template_renderer.rb:48:in block in render_template’
actionpack (4.0.0.beta1) lib/action_view/renderer/template_renderer.rb:56:in render_with_layout' actionpack (4.0.0.beta1) lib/action_view/renderer/template_renderer.rb:47:in render_template’
actionpack (4.0.0.beta1) lib/action_view/renderer/template_renderer.rb:17:in render' actionpack (4.0.0.beta1) lib/action_view/renderer/renderer.rb:36:in render_template’
actionpack (4.0.0.beta1) lib/action_view/renderer/renderer.rb:17:in render' actionpack (4.0.0.beta1) lib/abstract_controller/rendering.rb:119:in _render_template’
actionpack (4.0.0.beta1) lib/action_controller/metal/streaming.rb:219:in _render_template' actionpack (4.0.0.beta1) lib/abstract_controller/rendering.rb:112:in render_to_body’
actionpack (4.0.0.beta1) lib/action_controller/metal/rendering.rb:33:in render_to_body' actionpack (4.0.0.beta1) lib/action_controller/metal/renderers.rb:26:in render_to_body’
actionpack (4.0.0.beta1) lib/abstract_controller/rendering.rb:97:in render' actionpack (4.0.0.beta1) lib/action_controller/metal/rendering.rb:16:in render’
actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:41:in block (2 levels) in render' activesupport (4.0.0.beta1) lib/active_support/core_ext/benchmark.rb:5:in block in ms’
/home/john/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/benchmark.rb:296:in realtime' activesupport (4.0.0.beta1) lib/active_support/core_ext/benchmark.rb:5:in ms’
actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:41:in block in render' actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:84:in cleanup_view_runtime’
activerecord (4.0.0.beta1) lib/active_record/railties/controller_runtime.rb:24:in cleanup_view_runtime' actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:40:in render’
actionpack (4.0.0.beta1) lib/action_controller/metal/implicit_render.rb:10:in default_render' actionpack (4.0.0.beta1) lib/action_controller/metal/implicit_render.rb:5:in send_action’
actionpack (4.0.0.beta1) lib/abstract_controller/base.rb:189:in process_action' actionpack (4.0.0.beta1) lib/action_controller/metal/rendering.rb:10:in process_action’
actionpack (4.0.0.beta1) lib/abstract_controller/callbacks.rb:18:in block in process_action' activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:413:in _run__68927184__process_action__callbacks’
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in run_callbacks' actionpack (4.0.0.beta1) lib/abstract_controller/callbacks.rb:17:in process_action’
actionpack (4.0.0.beta1) lib/action_controller/metal/rescue.rb:29:in process_action' actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:31:in block in process_action’
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in block in instrument' activesupport (4.0.0.beta1) lib/active_support/notifications/instrumenter.rb:20:in instrument’
activesupport (4.0.0.beta1) lib/active_support/notifications.rb:158:in instrument' actionpack (4.0.0.beta1) lib/action_controller/metal/instrumentation.rb:30:in process_action’
actionpack (4.0.0.beta1) lib/action_controller/metal/params_wrapper.rb:245:in process_action' activerecord (4.0.0.beta1) lib/active_record/railties/controller_runtime.rb:18:in process_action’
actionpack (4.0.0.beta1) lib/abstract_controller/base.rb:136:in process' actionpack (4.0.0.beta1) lib/abstract_controller/rendering.rb:44:in process’
actionpack (4.0.0.beta1) lib/action_controller/metal.rb:196:in dispatch' actionpack (4.0.0.beta1) lib/action_controller/metal/rack_delegation.rb:13:in dispatch’
actionpack (4.0.0.beta1) lib/action_controller/metal.rb:232:in block in action' actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:78:in call’
actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:78:in dispatch' actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:46:in call’
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:69:in block in call' actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:57:in each’
actionpack (4.0.0.beta1) lib/action_dispatch/journey/router.rb:57:in call' actionpack (4.0.0.beta1) lib/action_dispatch/routing/route_set.rb:651:in call’
rack (1.5.2) lib/rack/etag.rb:23:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:in call’
rack (1.5.2) lib/rack/head.rb:11:in call' actionpack (4.0.0.beta1) lib/action_dispatch/middleware/params_parser.rb:27:in call’
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/flash.rb:241:in call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:in context’
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in call' actionpack (4.0.0.beta1) lib/action_dispatch/middleware/cookies.rb:452:in call’
activerecord (4.0.0.beta1) lib/active_record/query_cache.rb:36:in call' activerecord (4.0.0.beta1) lib/active_record/connection_adapters/abstract/connection_pool.rb:632:in call’
activerecord (4.0.0.beta1) lib/active_record/migration.rb:348:in call' actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:29:in block in call’
activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:373:in _run__125207171__call__callbacks' activesupport (4.0.0.beta1) lib/active_support/callbacks.rb:78:in run_callbacks’
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack (4.0.0.beta1) lib/action_dispatch/middleware/reloader.rb:64:in call’
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/remote_ip.rb:76:in call' actionpack (4.0.0.beta1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call’
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/show_exceptions.rb:30:in call' railties (4.0.0.beta1) lib/rails/rack/logger.rb:38:in call_app’
railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in block in call' activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in block in tagged’
activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:25:in tagged' activesupport (4.0.0.beta1) lib/active_support/tagged_logging.rb:67:in tagged’
railties (4.0.0.beta1) lib/rails/rack/logger.rb:21:in call' actionpack (4.0.0.beta1) lib/action_dispatch/middleware/request_id.rb:21:in call’
rack (1.5.2) lib/rack/methodoverride.rb:21:in call' rack (1.5.2) lib/rack/runtime.rb:17:in call’
activesupport (4.0.0.beta1) lib/active_support/cache/strategy/local_cache.rb:72:in call' rack (1.5.2) lib/rack/lock.rb:17:in call’
actionpack (4.0.0.beta1) lib/action_dispatch/middleware/static.rb:64:in call' railties (4.0.0.beta1) lib/rails/engine.rb:510:in call’
railties (4.0.0.beta1) lib/rails/application.rb:96:in call' railties (4.0.0.beta1) lib/rails/railtie/configurable.rb:30:in method_missing’
unicorn (4.6.2) lib/unicorn/http_server.rb:552:in process_client' unicorn (4.6.2) lib/unicorn/http_server.rb:632:in worker_loop’
unicorn (4.6.2) lib/unicorn/http_server.rb:500:in spawn_missing_workers' unicorn (4.6.2) lib/unicorn/http_server.rb:511:in maintain_worker_count’
unicorn (4.6.2) lib/unicorn/http_server.rb:277:in join' unicorn (4.6.2) bin/unicorn_rails:209:in <top (required)>’
/home/john/.rvm/gems/ruby-2.0.0-p0@rails-4/bin/unicorn_rails:23:in load' /home/john/.rvm/gems/ruby-2.0.0-p0@rails-4/bin/unicorn_rails:23:in <top (required)>’
ruby-debug-ide (0.4.17.beta16) lib/ruby-debug-ide.rb:86:in debug_load' ruby-debug-ide (0.4.17.beta16) lib/ruby-debug-ide.rb:86:in debug_program’
ruby-debug-ide (0.4.17.beta16) bin/rdebug-ide:109:in <top (required)>' -e:1:in load’
-e:1:in `’


## Request
**Parameters**:

{“id”=>“1”}

[Toggle session dump](http://localhost:3100/controller_actions/1/edit#)

[Toggle env dump](http://localhost:3100/controller_actions/1/edit#)

## Response
**Headers**:

None


Sorry about the verbosity but I am really struggling to solve this most basic use case.

I have tried to figure out why the form builder returns an array but and would write my own if I could figure out how to.

Any help appreciated.

Simple. You return an array from initialized_interactions.That’s why.

So what should be returned instead ?

Your question is wrong. You should not be rendering a form for :interactions (which will be an AR::Relation) using an array. You should rather be initializing the controller_action.interactions directly, and remove the second parameter in the fields_for call.

Hi, That is what happens now in models/controller_action.rb

def initialized_interactions() # this is the key method
  [].tap do |o|
    Role.all.each do |r|
      if p = interactions.find { |p| p.role_id == r.id }
        o << p.tap { |p| p.enable ||= true }
      else
        o << Interaction.new(:role_id => r.id)
      end
    end
  end
end

In rails 3.2 it just works. What should I return instead of an array ?

See https://github.com/ozpos/urps2 - use case 1