2

I've got some really weird stuff happening. In a view, I'm populating a collection like this:

<% foo_options = Bar.find(f.object.bar_id).supplier_contracts.collect(&:foo).uniq.compact : [] %>

This is a collection of ActiveRecord models.

EDIT: This happens in a form, so f.object here refers to another ActiveRecord model instance.

If I try dereferencing the collection's elements like this on the next line:

<%= foo_options.first.inspect %>

a valid model object gets inspected. Now, if I change it to

<%= foo_options.first.id %>

I get an exception "Called id for nil, which would mistakenly be 4". If I change it to

<%= foo_options.first.try :id %>

everything's OK, it displays the object's ID. Why does the exception happen?

HargrimmTheBleak
  • 2,147
  • 1
  • 19
  • 19
  • `f.object.bar_id` - You have an association called 'object' or am I missing something? If you do it's a really bad idea, 'object' is a reserved word in Rails and all kind of weird stuff can happen. – Art Shayderov May 28 '12 at 16:23
  • `f` is a form builder instance, so `f.object` references the actual model object bound to the form. Sorry for not making this clear. – HargrimmTheBleak May 28 '12 at 18:25

0 Answers0