我在bboy.rb模型中声明了has_many:rate。现在我想输出bboys表匹配条件中的所有记录。这是:根据他们收到的维度明星数量,按降序排序bboy记录:基础。
到目前为止,我在我的ActiveRecord排序查询中使用desc顺序进行维度:基础: @bboys_foundation = Bboy.rates.where(维度:'基础')。?
在bboys / index.html.erb中:
<ul class="dropdown-menu">
<li><a href="?sort=foundation">Foundation</a></li>
<li><a href="?sort=originality">Originality</a></li>
<li><a href="?sort=dynamics">Dynamics</a></li>
<li><a href="?sort=Execution">Execution</a></li>
<li><a href="?sort=Battle">Battle</a></li>
<li><a href="?sort=Votes">Votes</a></li>
</ul>
在bboys_controller索引方法中:
if params[:sort].present?
sort_input = ['foundation', 'originality', 'dynamics', 'execution', 'battle', 'votes']
@foundation = if sort_input[0].include?
end
@originality = if sort_input[1].include?
end
@dynamics = if sort_input[2].include?
end
@execution = if sort_input[3].include?
end
@battle = if sort_input[4].include?
end
@votes = if sort_input[5].include?
end
db schema:
ActiveRecord::Schema.define(version: 20140331112614) 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 "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