简体   繁体   English

Rails ActiveRecord查询排序2个模型?

[英]Rails ActiveRecord query sorting 2 models?

This is my specific question. 这是我的具体问题。 I need to display in view only the records from bboys table that match the criteria. 我只需要在视图中显示bboys表中符合条件的记录。 The criteria is: records based on stars count for 'dimension: originality' in descending order. 标准是:基于星的记录按降序计数“维:原创性”。 rateable_id: 3 in rates table is same as id: in bboys table. rateable_id:费率表中的3与bboys表中的id:相同。 Any advice is greatly appreciated! 任何意见是极大的赞赏! I am new to querying with AciveRecord, so any info I could build on would be great. 我是使用AciveRecord进行查询的新手,因此我可以建立的任何信息都很棒。

Table rates: 桌费:

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

Table bboys: 表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

This will give you an ActiveRecord::Relation with Bboy objects with bboys.id=3 and rates.dimension = 'originality' , ordered by rates.star in descending order. 这将为您提供一个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