ruby on rails - Basic ActiveModel methods not working on objects -
i asked question earlier calling associated model attributes.
specifically, asked trying display names of vip model instances associated event.
initially had
<% @organization.events.each |event| %> <p> <table> <tr> <td> <%= event.name %> </td> <td> <%= event.vips.name %> </td> </tr> </table> </p> <% end %>
which changed
<%= event.vips.first.name %>
and worked little while. adjusted
<% event.vips.each |vip| %> <%= vip.name %>
which worked little while. when came computer after taking break, new event form submissions no longer displaying vip's name on organization show page, though database being updated vip_id foreign key.
in fact, when tried
<%= event.vips.first.name %>
again, got error saying "undefined method `name' nil:nilclass"
i'm not sure changed, or i'm doing wrong. appreciated.
update:
to clarify problem, following outputs console:
irb(main):005:0> event.first event load (0.2ms) select "events".* "events" order "events"."id" asc limit 1 => #<event id: 1, when: "2015-08-25 00:00:00", organization_id: 1, created_at: "2015-08-25 04:47:43", updated_at: "2015-08-25 04:47:43", name: "john's event", vip_id: 1> irb(main):006:0> vip.first vip load (0.2ms) select "vips".* "vips" order "vips"."id" asc limit 1 => #<vip id: 1, name: "john", created_at: "2015-08-25 04:46:23", updated_at: "2015-08-25 04:46:23", organization_id: 1, event_id: nil> irb(main):007:0> @organization => #<organization id: 1, name: "test org", created_at: "2015-08-25 04:46:03", updated_at: "2015-08-25 04:46:03", user_id: 1> irb(main):008:0> @organization.events.first => #<event id: 1, when: "2015-08-25 00:00:00", organization_id: 1, created_at: "2015-08-25 04:47:43", updated_at: "2015-08-25 04:47:43", name: "john's event", vip_id: 1> irb(main):009:0> @organization.events.first.vips vip load (0.2ms) select "vips".* "vips" "vips"."event_id" = ? [["event_id", 1]] => #<activerecord::associations::collectionproxy []> irb(main):010:0>
your event
has no vips
. since event.vips
empty array, calling #first
on returns nil
. try call #name
on nil
. #name
isn't method of nil
, hence error undefined method 'name' nil:nilclass
.
Comments
Post a Comment