As another example I can also put in the same view:
<% @states.each do |state| %>
<% state.coordinators.each do |coordinator| %>
<p><%= coordinator.county.name %> </p>
<% end %>
<% end %>
and I get
undefined method `name' for nil:NilClass
On May 26, 10:09 am, Aaron Mills <[email protected]> wrote:
> Woops in all my changes I put name instead of firstname on my
> coordinators.. This is now correct but the original issue is still
> there.. which is when I try and call state.county.name or
> coordinator.state.name or county.state.name.
>
> I am quite sure that county.name exists and that state.name exists.
>
> I ran some commands in the console, here is what I get:
>
> examplestate = State.find(1)
> => #<State id: 1, name: "Alabama", abreviation: "AL", created_at:
> "2011-04-04 16:32:08", updated_at: "2011-04-04 16:32:08">
>
> examplestate.name
> => "Alabama"
>
> examplecounties = examplestate.counties
> => [#<County id: 3, name: "Baldwin County", state_id: 1, created_at:
> "2011-04-04 16:32:32", updated_at: "2011-04-04 16:32:32">, #<County
> id: 4, name: "Cherokee County", state_id: 1, created_at: "2011-04-04
> 16:32:32", updated_at: "2011-04-04 16:32:32">, #<County id: 5, name:
> "Chilton County", state_id: 1, created_at: "2011-04-04 16:32:33",
> updated_at: "2011-04-04 16:32:33">, #<County id: 6, name: "Clarke
> County", state_id: 1, created_at: "2011-04-04 16:32:33", updated_at:
> "2011-04-04 16:32:33">, #<County id: 7, name: "Colbert County",
> state_id: 1, created_at: "2011-04-04 16:32:33", updated_at:
> "2011-04-04 16:32:33">, #<County id: 8, name: "Crenshaw County",
> state_id: 1, created_at: "2011-04-04 16:32:33", updated_at:
> "2011-04-04 16:32:33">, #<County id: 9, name: "Cullman County",
> state_id: 1, created_at: "2011-04-04 16:32:33", updated_at:
> "2011-04-04 16:32:33">, #<County id: 10, name: "DeKalb County",
> state_id: 1, created_at: "2011-04-04 16:32:33", updated_at:
> "2011-04-04 16:32:33">,etc.. etc..
>
> examplecounties.each do |county|
> puts county.coordinator.firstname
> end
>
> Jessica
> Ron
> P. Dale
> Gerry
> Garry
> Tami
> Samuel
> etc. etc.
>
> So when I call a coordinators firstname from a county, from a state..
> it seems to work.. but then when I try that in the view for instance:
>
> My state controller calls @state.all
>
> and in the state index view I have:
>
> <% @states.each do |state| %>
> <strong><%= state.name %></strong>
> <% state.counties.each do |county| %>
> <p><%= county.coordinator.firstname %> </p>
> <% end %>
> <% end %>
>
> I get this error:
> undefined method `firstname' for nil:NilClass
>
> On May 26, 3:42 am, Michael Pavling <[email protected]> wrote:
>
>
>
>
>
>
>
> > On 26 May 2011 07:07, Aaron Mills <[email protected]> wrote:
>
> > > I am getting:this error. I have tried several variations on this
> > > theme.. so I feel like I am stuck in my understanding of how rails
> > > associations work. Can someone point me in the right direction?
> > > undefined method `name' for "#<Coordinator:
> > > 0x00000003a83128>":Coordinator
>
> > That's a fairly straightforward, self-explanatory error, that's got
> > nothing to do with associations.
> > It's telling you that Coordinator has no method "name". Have you
> > created and run a migration to add a field called "name" to that
> > table? Can you check in your DB management tool to check the field is
> > there. Can you open a console and execute "Coordinator.new" (or just
> > "Coordinator") and see the list of available attributes/field names.
>
> > Let us know the result.
> > Regards,
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.