簡體   English   中英

Rails ActiveRecord查詢排序2個模型?

[英]Rails ActiveRecord query sorting 2 models?

這是我的具體問題。 我只需要在視圖中顯示bboys表中符合條件的記錄。 標准是:基於星的記錄按降序計數“維:原創性”。 rateable_id:費率表中的3與bboys表中的id:相同。 任何意見是極大的贊賞! 我是使用AciveRecord進行查詢的新手,因此我可以建立的任何信息都很棒。

桌費:

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

這將為您提供一個ActiveRecord::Relation與具有bboys.id=3rates.dimension = 'originality' Bboy對象的ActiveRecord::Relation ,並由rates.star降序排列。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM