user3222947
user3222947

Reputation: 197

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. 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. Any advice is greatly appreciated! I am new to querying with AciveRecord, so any info I could build on would be great.

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:

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

Upvotes: 0

Views: 35

Answers (1)

vee
vee

Reputation: 38645

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.

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

Upvotes: 1

Related Questions