我只想在<th>User Votes</th>
下的index.html.erb中显示有多少用户投票支持bboy?
这是在我的控制器中:
@raters_count = Rate.select(:rater_id).where(rateable_id: 3, rateable_type: 'Bboy' ).distinct.count
这是我的index.html.erb:
<h1>Listing bboys</h1>
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>Name</th>
<th>Crew</th>
<th>Country</th>
<th>Foundation</th>
<th>Originality</th>
<th>Dynamics</th>
<th>Execution</th>
<th>Battle</th>
<th>User Votes</th>
</tr>
</thead>
<tbody>
<% @bboys.each do |bboy| %>
<tr>
<td><%= bboy.name %></td>
<td><%= bboy.crew %></td>
<td><%= bboy.country %></td>
<td><%= rating_for bboy, "foundation" %></td>
<td><%= rating_for bboy, "originality" %></td>
<td><%= rating_for bboy, "dynamics" %></td>
<td><%= rating_for bboy, "execution" %></td>
<td><%= rating_for bboy, "battle" %></td>
<td><%= @raters_count %></td>
<td><%= link_to 'Show', bboy %></td>
<td><%= link_to 'Edit', edit_bboy_path(bboy) %></td>
<td><%= link_to 'Destroy', bboy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Bboy', new_bboy_path %>
这是我的表bboys:
- !ruby/object:Bboy
attributes:
id: 3
name: taisuke
crew: japan
country: JAP
created_at: 2014-02-25 09:32:56.000000000 Z
updated_at: 2014-02-25 09:32:56.000000000 Z
avatar_file_name: justdoit.jpeg
avatar_content_type: image/jpeg
avatar_file_size: 9297
avatar_updated_at: 2014-02-25 09:32:55.000000000 Z
- !ruby/object:Bboy
attributes:
id: 4
name: roxrite
crew: us
country: us
created_at: 2014-02-25 09:35:40.000000000 Z
updated_at: 2014-02-25 09:35:40.000000000 Z
avatar_file_name:
avatar_content_type:
avatar_file_size:
avatar_updated_at:
- !ruby/object:Bboy
attributes:
id: 5
name: me
crew: japan
country: JAP
created_at: 2014-02-25 09:36:23.000000000 Z
updated_at: 2014-02-25 09:36:23.000000000 Z
avatar_file_name: justdoit.png
avatar_content_type: image/png
avatar_file_size: 91715
avatar_updated_at: 2014-02-25 09:36:22.000000000 Z
- !ruby/object:Bboy
attributes:
id: 6
name: asd
crew: asd
country: asd
created_at: 2014-03-04 13:24:41.000000000 Z
updated_at: 2014-03-04 13:24:41.000000000 Z
avatar_file_name: images.jpeg
avatar_content_type: image/jpeg
avatar_file_size: 7928
avatar_updated_at: 2014-03-04 13:24:40.000000000 Z
- !ruby/object:Bboy
attributes:
id: 8
name: name
crew: name
country: name
created_at: 2014-03-04 14:39:20.000000000 Z
updated_at: 2014-03-04 14:39:20.000000000 Z
avatar_file_name:
avatar_content_type:
avatar_file_size:
avatar_updated_at:
这是我的表费率:
- !ruby/object:Rate
attributes:
id: 11
rater_id: 1
rateable_id: 3
rateable_type: Bboy
stars: 5.0
dimension: foundation
created_at: 2014-02-25 09:33:23.000000000 Z
updated_at: 2014-02-25 09:33:23.000000000 Z
- !ruby/object:Rate
attributes:
id: 12
rater_id: 1
rateable_id: 3
rateable_type: Bboy
stars: 5.0
dimension: originality
created_at: 2014-02-25 09:33:24.000000000 Z
updated_at: 2014-02-25 09:33:24.000000000 Z
- !ruby/object:Rate
attributes:
id: 13
rater_id: 1
rateable_id: 3
rateable_type: Bboy
stars: 5.0
dimension: dynamics
created_at: 2014-02-25 09:33:25.000000000 Z
updated_at: 2014-02-25 09:33:25.000000000 Z
- !ruby/object:Rate
attributes:
id: 14
rater_id: 1
rateable_id: 3
rateable_type: Bboy
stars: 5.0
dimension: execution
created_at: 2014-02-25 09:33:26.000000000 Z
updated_at: 2014-02-25 09:33:26.000000000 Z
- !ruby/object:Rate
attributes:
id: 15
rater_id: 1
rateable_id: 3
rateable_type: Bboy
stars: 5.0
dimension: battle
created_at: 2014-02-25 09:33:27.000000000 Z
updated_at: 2014-02-25 09:33:27.000000000 Z
- !ruby/object:Rate
attributes:
id: 16
rater_id: 1
rateable_id: 5
rateable_type: Bboy
stars: 5.0
dimension: foundation
created_at: 2014-02-25 09:36:30.000000000 Z
updated_at: 2014-02-25 09:36:30.000000000 Z
- !ruby/object:Rate
attributes:
id: 17
rater_id: 1
rateable_id: 5
rateable_type: Bboy
stars: 5.0
dimension: originality
created_at: 2014-02-25 09:36:31.000000000 Z
updated_at: 2014-02-25 09:36:31.000000000 Z
- !ruby/object:Rate
attributes:
id: 18
rater_id: 1
rateable_id: 5
rateable_type: Bboy
stars: 5.0
dimension: dynamics
created_at: 2014-02-25 09:36:31.000000000 Z
updated_at: 2014-02-25 09:36:31.000000000 Z
- !ruby/object:Rate
attributes:
id: 19
rater_id: 1
rateable_id: 5
rateable_type: Bboy
stars: 5.0
dimension: battle
created_at: 2014-02-25 09:36:32.000000000 Z
updated_at: 2014-02-25 09:36:32.000000000 Z
- !ruby/object:Rate
attributes:
id: 25
rater_id: 8
rateable_id: 3
rateable_type: Bboy
stars: 1.0
dimension: foundation
created_at: 2014-03-04 14:06:46.000000000 Z
updated_at: 2014-03-04 14:06:46.000000000 Z
- !ruby/object:Rate
attributes:
id: 26
rater_id: 8
rateable_id: 3
rateable_type: Bboy
stars: 1.0
dimension: originality
created_at: 2014-03-04 14:06:49.000000000 Z
updated_at: 2014-03-04 14:06:49.000000000 Z
- !ruby/object:Rate
attributes:
id: 27
rater_id: 8
rateable_id: 3
rateable_type: Bboy
stars: 1.0
dimension: dynamics
created_at: 2014-03-04 14:06:51.000000000 Z
updated_at: 2014-03-04 14:06:51.000000000 Z
- !ruby/object:Rate
attributes:
id: 28
rater_id: 8
rateable_id: 3
rateable_type: Bboy
stars: 1.0
dimension: execution
created_at: 2014-03-04 14:06:53.000000000 Z
updated_at: 2014-03-04 14:06:53.000000000 Z
- !ruby/object:Rate
attributes:
id: 29
rater_id: 8
rateable_id: 3
rateable_type: Bboy
stars: 1.0
dimension: battle
created_at: 2014-03-04 14:06:54.000000000 Z
updated_at: 2014-03-04 14:06:54.000000000 Z
Schema.rb:
ActiveRecord::Schema.define(version: 20140225090908) do
create_table "bboys", force: true do |t|
t.string "name"
t.string "crew"
t.string "country"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
create_table "bonghis", force: true do |t|
t.string "name"
t.string "email"
t.integer "age"
t.text "summary"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "rates", force: true do |t|
t.integer "rater_id"
t.integer "rateable_id"
t.string "rateable_type"
t.float "stars", null: false
t.string "dimension"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rates", ["rateable_id", "rateable_type"], name: "index_rates_on_rateable_id_and_rateable_type", using: :btree
add_index "rates", ["rater_id"], name: "index_rates_on_rater_id", using: :btree
create_table "rating_caches", force: true do |t|
t.integer "cacheable_id"
t.string "cacheable_type"
t.float "avg", null: false
t.integer "qty", null: false
t.string "dimension"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "rating_caches", ["cacheable_id", "cacheable_type"], name: "index_rating_caches_on_cacheable_id_and_cacheable_type", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
答案 0 :(得分:1)
在Rate
模型中添加范围,如下所示:
class Rate < ActiveRecord::Base
scope :raters_count, ->(rateable_id) { select(:rater_id).where(rateable_id: rateable_id, rateable_type: 'Bboy' ).distinct.count }
end
在视图中,按以下方式访问它:
<td><%= Rate.raters_count(bboy.id) %></td>
而不是
<td><%= @raters_count %></td>
此外,从控制器中删除@raters_count
。