nested_form_fields icon indicating copy to clipboard operation
nested_form_fields copied to clipboard

Cannot access attributes of nested object

Open wjdhamilton opened this issue 9 years ago • 12 comments

When trying to gain access to the attributes of an associated object whilst updating its parent, it is possible to access the associated object, but not its attributes. For instance, the following works:

<%= f.nested_fields_for :charges do |cf| %>
          <%= cf.object.account %> 
      <% end %>

Whereas this fails with undefined method 'id' for nil:NilClass:

<%= f.nested_fields_for :charges do |cf| %>
          <%= cf.object.account.id %> 
 <% end %>

Laws of Demeter aside, this feels profoundly odd.

wjdhamilton avatar Dec 23 '15 11:12 wjdhamilton

It does indeed :) what does cf.object give you exactly? A Charge instance would be expected.

ncri avatar Dec 23 '15 13:12 ncri

Yes, it gives a Charge instance. And the correct one too! It's as though Rails doesn't go to the database to find associated objects if they haven't been loaded.

wjdhamilton avatar Dec 23 '15 17:12 wjdhamilton

I have never seen this behavior. most likely it has nothing to do with nested_form_fields, as nested_form_fields simply sits on top of the basic rails form builder. Can you try to remove nested_form_fields and see if the issue is still there?

ncri avatar Dec 23 '15 19:12 ncri

I can confirm that the issue is not there if I use fields_for.

wjdhamilton avatar Dec 23 '15 20:12 wjdhamilton

oh, that is really odd... i need to check if nested_form_fields does anything different here than fields_for... strange, i didn't expect that

ncri avatar Dec 23 '15 20:12 ncri

oops, sorry for the unrelated (now deleted) comment, i accidentally replied to the wrong email ;-)

On Wed, Dec 23, 2015 at 10:43 PM, Nico Ritsche [email protected] wrote:

they are blocked now :) they wont get through to the app, just see a "forbidden" page

On Wed, Dec 23, 2015 at 10:17 PM, wjdhamilton [email protected] wrote:

I can confirm that the issue is not there if I use fields_for.

— Reply to this email directly or view it on GitHub https://github.com/ncri/nested_form_fields/issues/55#issuecomment-166983389 .

ncri avatar Dec 23 '15 20:12 ncri

LOL I was struggling to put that one into context!

wjdhamilton avatar Dec 23 '15 20:12 wjdhamilton

happened to me the first time ;-)

ncri avatar Dec 23 '15 20:12 ncri

Well, at least for now you can use a workaround:

- account_id = cf.object.account_id

or, if you need the whole account object:

- account = Account.find(cf.object.account_id)

ncri avatar Dec 23 '15 20:12 ncri

I swear I tried the first option without success but it's working now! Thanks for your help and attention :+1:

wjdhamilton avatar Dec 24 '15 09:12 wjdhamilton

I'm currently facing the same problem. It only works on 1 parent model association, the other two, no luck:

<%= form.nested_fields_for :delivery_items, wrapper_tag: :tr do |ff| %>
  <td class="col-md-1">
    <div class="input-product_code-div">
        <%= ff.object.product.name  %> <br> #works!
        <%= ff.object.sales_order_item.address  %> <br> #does not work
        <%= ff.object.delivery.id  %> #does not work
        <%end%>
    </div>
  </td>
<% end %>

bassman88 avatar Nov 02 '17 07:11 bassman88

@bassman88: does the workaround work for you?

ncri avatar Nov 05 '17 09:11 ncri