2014-04-03 67 views
0

這是我的具體問題。我只需要顯示符合條件的bboys表中的記錄。標準是:基於星號的記錄按降序計數'維度:原創性'。 rateable_id:rate表中的3與id:在bboys表中相同。任何意見是極大的讚賞!我是使用AciveRecord查詢的新手,所以我可以建立的任何信息都會很棒。Rails的ActiveRecord查詢排序2模型?

表率:

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

表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 

回答

1

這會給你Bboy對象與bboys.id=3rates.dimension = 'originality',通過rates.star降序排序的ActiveRecord::Relation

Bboy.joins(:rates) 
    .where('rates.dimension = ? and bboys.id = ?', 'originality', 3) 
    .order('rates.star desc') 
+0

可否請你看看我的代碼在這裏:http://stackoverflow.com/questions/22840627/rails-activerecord-model-sorting-how我只需要它是如何都應該概述工作?乾杯 – user3222947