0

Excuse me for the seemingly basic question, but I'm new to Rails and I can't seem to google anything useful.

I have two models: Works has_many Pictures.

Both models have a :title.

If, in my works/index.html.erb file, I include:

<%= work.title %>

The title of the Work shows up as expected.

Likewise, if I include:

<%= work.pictures[0] %>

The class id of the first related record from Pictures shows up, because (I believe) they are correctly linked via has_many and belongs_to.

But if I do this instead:

<%= work.pictures[0].title %>

I get a NoMethodError: undefined method 'title' for nil:NilClass

I know the Pictures model has a title field. Obviously I'm not doing it right; what am I missing?

Thanks!

4

1 回答 1

0

问题在于记录ID不准确...

Works.pictures[0].title

...确实有效。

于 2013-05-07T23:43:12.280 回答