0
所以这里就是我想: 对于表中的每一个艺术家,我希望它发布它的名字和他做最后的三幅作品。获得来自艺术家的所有作品的标题用Rails
这是我到目前为止有:
class Artist < ActiveRecord::Base
attr_accessible :image, :name
has_many :works
end
class Work < ActiveRecord::Base
attr_accessible :artist_id, :exhibition_id, :image, :title
belongs_to :artist
belongs_to :exhibition
end
<% @artists.each do |artist| %>
<div class="one-artist">
<h3><%= artist.name %></h3>
<div class="artist-work first-work">
<%= artist.works.title %>
</div>
<% end %>
我想这将是接近艺术家可接受的方式工作,但似乎失败。
它给了我下面的repsonse:
undefined method `title' for #<ActiveRecord::Relation:0x00000102e89038>
谢谢,我明白了你的观点,但我找不出解决方案。我如何才能达到一项工作? – polonium 2012-07-07 15:12:05
您应该遍历所有的作品使用每个,或者,如果你只想要第一个使用我上面写的代码:artist.works.first.title – davidrac 2012-07-07 15:14:23
谢谢!有用。 – polonium 2012-07-07 15:20:27