0

だからここに私が取得したいものがあります: テーブル内の各アーティストについて、その名前と彼が作成した最近の 3 つの作品を投稿する必要があります。

これが私がこれまでに持っているものです:

    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.each do |work| %>
    <div class="artist-work">  
        <%= work.title %>
    </div>
<% end %>
于 2012-07-07T15:19:02.550 に答える
1

works はすべての作品を含む関係です。たとえば、最初にアクセスしたい場合は、次のように書く必要がありますartist.works.first.title

于 2012-07-07T15:02:24.327 に答える