0

名前、日付、スコア、グレードの 4 つの列を持つテーブルを設定しました。表全体で、名前カテゴリに多くの重複があります。名前をクリックすると、そのユーザーのすべてのスコア、グレード、およびそれぞれの日付が表示されます。

現在、テーブルに複数回表示される名前をクリックすると、選択したインスタンスのスコア、グレード、および日付のみが表示されます。その名前に関連付けられているすべてのスコア、グレード、および日付を表示するにはどうすればよいですか。

ここにデータベースのスナップショットがあります -

スコア表:

class CreateScores < ActiveRecord::Migration
   def change
     create_table :scores do |t|
       t.string :name   
       t.integer :score
       t.date :fielded
       t.string :grade
       t.integer :name_id

       t.timestamps
     end
   end
 end

生徒のテーブル:

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.integer :id
      t.string :name

      t.timestamps
    end
  end
end

これが私のビューに表示されるテーブルです-

<% @score.each do |score| %>
  <tr class="tableline" >
     <td class="tableline"><%= link_to score.name.upcase, score, {style: 'color:#FFFFFF; text-decoration: none; opacity:1'} %></td>
     <td class="tableline"><%= score.fielded %></td>
     <td class="tableline"><%= score.score %></td>
     <td class="tableline"><%= score.grade %></td>
  </tr>
<% end %>
4

1 に答える 1

0

it seems like you need some re-factoring to your tables as well, as an example you should probably having a users table and a related grades table.

Ex:

#users table
id
name

#grades table
id
user_id
score
grade
date

active record models

class User < ActiveRecord::Base
  has_many :grades
end

class Grade < ActiveRecord::Base
  belongs_to :user
end

to get all the grades to for a user

user = User.find(id)
user.grades
于 2013-03-11T07:15:54.030 に答える