Question

I've got 3 models that comprise a has-many-through association.

Model code as follows:

ItemAttrVal Model (the transition table)

class ItemAttrVal < ActiveRecord::Base
  belongs_to :attr_name
  belongs_to :registry_item
end

RegistryItem Model

class RegistryItem < ActiveRecord::Base
  has_many :item_attr_vals
  has_many :attr_names, :through => :item_attr_vals
  accepts_nested_attributes_for :item_attr_vals, :allow_destroy => :true
end

AttrName Model

class AttrName < ActiveRecord::Base
  has_many :item_attr_vals
  has_many :registry_items, :through => :item_attr_vals
end

The RegistryItem uses a fields_for as follows:

<%= item.fields_for :item_attr_vals do |iav| %>
    <%= render 'item_attr_val_fields', :f => iav %>
<% end %>

In the partial, it looks like this:

<% logger.debug "object type is: #{f.object}"%>
<% logger.debug "some details are: #{f.object.attr_name_id}--"%>
<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description"), :selected => f.object.attr_name_id, :prompt => "Select an attribute" %>
<%= f.text_field :raw_value %> <br />

The 1st 2 debug lines are the bit that my question is about, but it first relates to the 3rd line. There, I am attempting to provide the dropdown select field with a "pre-selected" value. This is so that when the user is editing the RegistryItem, their previously selected AttrName will be displayed.

I'm attempting to use the f.object.attr_name_id to set that value, however it does not actually properly select the previously selected value, and instead, just goes to the 1st.

The 1st two debug lines were then me trying to make sure that my f.object method worked...

When I looked in my logs, I see the following:

object type is: #<ItemAttrVal:0x007fb3ba2bd980>
some details are: --

Basically, the 1st line shows me that I am getting the ItemAttrVal The second line does not seem to retrieve any information for it.

I've also used the debugger to check, and in there, I am able to use display f.object.attr_name_id to show me the exact value that I'm expecting...

This kind of comes down to two questions...

  1. Why can't I retrieve the values of f.object?
  2. Am I trying to do line 3 (<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description"), :selected => f.object.attr_name_id, :prompt => "Select an attribute" %>) wrong, and there's actually a better way to do it?

Thanks in advance!

Was it helpful?

Solution 2

Turns out I'd placed the :selected in the wrong location...

Original:

<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description"), :selected => f.object.attr_name_id, :prompt => "Select an attribute" %>

Should be:

<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description", f.object.attr_name_id), :prompt => "Select an attribute" %>

Fixing that solved my problem, the attribute names are now appearing as expected for previously saved attributes.

It still doesn't answer my original query about why I'm not able to get the values for f.object printed out, but at least the original-original problem was resolved.

OTHER TIPS

you need to use params[:attr_name_id] into your options_from_collection_for_select

<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description", params[:attr_name_id].to_i), :prompt => "Select an attribute" %>

hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top