Person has many MiddleNames
and
MiddleName belongs to Person
In irb I can add a person, add a middle name then call
@person.middle_names.first.name
to get that middle name. However when I try the exact same code in a view, I
get
undefined method `name' for nil:NilClass
If I iterate through like so:
for mname in @person.middle_names
puts mname.name
Then it works perfectly, but for some reason using ".first" is failing in
the view. I've used this method many many times and never come across
anything quite like this.
I believe I'm probably overlooking some incredibly simple answer and I'm
going to kick myself when someone points it out, but for now I'm completely
stumpted.
Then it works perfectly, but for some reason using ".first" is failing
in
the view. I've used this method many many times and never come across
anything quite like this.
".first" is an array method. "mname.name" is an instance of an array...
The error I am getting only appears from my testing with cucumber/rspec/webrat. If I
access the page manually through passenger, it all works perfectly.
I might contact the cuke list about this as the code is obviously correct, it works
fine in irb and in a real browser, just not in the test environment. So much for me
sticking to my BDD plans
> I've got a relationship:
>
> Person has many MiddleNames
> and
> MiddleName belongs to Person
>
>
> In irb I can add a person, add a middle name then call
>
> @person.middle_names.first.name
And what happens if you don't add a middle name.
.first #=> nil
That is true, but in my scenario I have added a middle name and related it to that
person. I will of course go and triple check that but I have executed the copy and
pasted commands from the cuke steps and it worked in irb.
Its quite possible I have slipped up but I will go and check. Thanks
>
> to get that middle name. However when I try the exact same code in a view, I
> get
>
> undefined method `name' for nil:NilClass
>
> If I iterate through like so:
>
> for mname in @person.middle_names
> ?puts mname.name
Because if @person.middle_names is empty the loop body is not executed.
But it does execute the loop, and prints out the first of the names as expected. This
is the weird thing.