0

所以这就是我想要得到的:对于表格中的每个艺术家,我希望它发布它的名字和他最近创作的三件作品。

这是我到目前为止所拥有的:

    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 %>

我认为这是一种可以接受的接近艺术家作品的方式,但它似乎失败了。

它给了我以下回复:

undefined method `title' for #<ActiveRecord::Relation:0x00000102e89038>
4

2 回答 2

1

作品是包含所有作品的关系。如果你想访问,比如说,第一个你应该写类似artist.works.first.title

于 2012-07-07T15:02:24.327 回答
1
<% artist.works.each do |work| %>
    <div class="artist-work">  
        <%= work.title %>
    </div>
<% end %>
于 2012-07-07T15:19:02.550 回答