Melvin
Melvin

Reputation: 376

Order by A then B using Ruby on Rails Model

This is not a homework question. I am trying to learn more.

I have the following entities with attributes
Manufacturer {name} //Store Manufactueres
Model {manufacturer_id, name} //Store Models
Tint {manufacturer_id, model_id, front, side, rear} //Store measurements

I have the follow data in my Tint entity. Alphabets stands for different manufacturer name and models.

Manufacturer | Model | Front | Side | Rear | 
-------------+-------+-------+------+-------
A            | AD    | 10    | 10   | 10   |
B            | AB    | 10    | 10   | 10   |
A            | AA    | 10    | 10   | 10   |
A            | AC    | 10    | 10   | 10   |
B            | AA    | 10    | 10   | 10   |
A            | AB    | 10    | 10   | 10   |

When I print it out in view, I would like to have it sorted based on Manufacturer name and then Model. So the result will be as below. The name of the Manufactures will be sorted alphabetically, then Models.

Manufacturer | Model | Front | Side | Rear | 
-------------+-------+-------+------+-------
A            | AA    | 10    | 10   | 10   |
A            | AB    | 10    | 10   | 10   |
A            | AC    | 10    | 10   | 10   |
A            | AD    | 10    | 10   | 10   |
B            | AA    | 10    | 10   | 10   |
B            | AB    | 10    | 10   | 10   |

I have setup the model to make sure Manufacturer and Model is a distinct pair of values.

My question is since I am referencing using manufacturer_id and model_id, how can I get the name of the Manufacturer and Model from Manufacturer and Model table.

In my tints_controller.rb, I have @tints = Tint.all.order(:manufacturer_id). However, it will only sort based on the manufacturer_id (as in numbers) instead of the name of the manufacturer.

I know that I can do it in SQL way (SELECT, FROM, WHERE) in RoR model. However, I would like to know is it possible to use ActiveRecord to sort the data based on their name.

Upvotes: 0

Views: 378

Answers (4)

Melvin
Melvin

Reputation: 376

Answer to my question:

In tints_controller.rb, I wrote @tints = Tint.joins(:manufacturer, :model).order("manufacturers.name ASC, models.name ASC") to join the table and order them accordingly.

I tried the answer provided by @Goston above and I had an issue when I was trying edit the tints. It did not allow me to edit.

Note: Answer provided by @Goston will order them, but it broke the edit function for my case.

Upvotes: 0

Hasmukh Rathod
Hasmukh Rathod

Reputation: 1118

In your tints_controller.rb, instedad of

@tints = Tint.all.order(:manufacturer_id)

please write:

@tints = Tint.all.order(:manufacturer_id, :model_id) 

Upvotes: 0

Gaston
Gaston

Reputation: 994

If I understand correctly, you have 3 models, Tint, Manufacturer and Model. I am assuming you have the appropiate has_many and belongs_to associations setup correctly.

Tint.rb
belongs_to :workspace

Manufacturer.rb
has_many :models
has_many :tints,  through: :models

Model.rb:
belongs_to Manufacturer
has_many :tints

You need to first join the three models together, and then order by some criteria

tints_controller.rb
@tints = Tint.joins(model: :manufacturer).order('manufacturers.name, models.name').pluck('manufacturers.name, models.name, tints.front, tints.side, tints.rear')

That will give you all tints records and they appropiate models and manufacturers.

Upvotes: 2

MarsAtomic
MarsAtomic

Reputation: 10696

Any time you have the id of an entity in Rails, you can easily retrieve other associated fields simply by instantiating that entity:

@manufacturer = Manufacturer.find(params[manufacturer_id])

Then it's a simple matter to retrieve any of the other fields:

@manufacturer_name = @manufacturer.name

If you need a collection of manufacturers or manufacturer names, then it's advisable to build yourself an ActiveRecord::Relation object immediately via a scoped query (as you already know). I have no idea what your criteria are, otherwise, I'd supply some sample code. I can tell you that your scoped query should include an .order clause at the end:

@manufacturers = Manufacturer.where("some_column = ?", some_criterion).order(:sort_field)

In the above example, :sort_field would be the field by you want to sort your ActiveRecord::Relation. I'm guessing in your case, it's :name.

All this having been said, if you want fancy sorted tables, you should look into the JQuery DataTables gem. DataTables can do a lot of the heavy lifting for you, and it's convenient for your users because they can then sort and resort by any column you present.

Upvotes: 0

Related Questions