How to create a Rails model from a subset of table entries

I am trying to create several models that pull from one table. How to limit table entries in each model? And before you tell me to change my data structure, this is a reporting application that pulls from an existing backup database that I have no control over.

My table looks something like this:

Vehicle_Table id vehicle_type name -------------------- 1 Car Foo 2 Car Bar 3 Motorcycle Baz 4 Car Barf 

And I want to build models for cars and motorcycles like:

 class Car < ActiveRecord::Base set_table_name 'Vehicle_Table' end 

and

 class Motorcycle < ActiveRecord::Base set_table_name 'Vehicle_Table' end 

But I have no idea how to say: "Hi, Active Record, I only need records where car_type = motorcycle in the motorcycle model."

I'm sure this is obvious, but all of my Google searches return paths to subsets of FIND in the model, and not to RESTRICT models to a specific subset of records.

+4
source share
2 answers

This is called single page inheritance (STI).

If your table had a column named type , it most likely will work automatically. But you can change this column name, which Rails uses to separate types.

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Inheriting a single table

Active Record allows you to inherit by storing the class name in a column called "type" by default (can be changed by overwriting Base.inheritance_column). This means that inheritance is as follows:

 class Company < ActiveRecord::Base; end class Firm < Company; end class Client < Company; end class PriorityClient < Client; end 

When you execute Firm.create (: name => "37signals"), this entry will be saved in the company table with type = "Firm". Then you can take this line again using Company.where (: name => '37signals'). First, it will return a Firm object.

So try this code

 class Car < ActiveRecord::Base set_table_name 'Vehicle_Table' self.inheritance_column = :vehicle_type end 
+4
source

Commented above, but had limited editing capabilities. I ran into this exact problem and found the other half of the solution elsewhere. STI will let you get a subset of a table based on a column in a table, but it will drop the class name to find records for that class. For instance:

 class Company < ActiveRecord::Base; end class Client < Company; end 

This will look at a table named Company for records that have a value of β€œClient” in a column named β€œType”.

You can override the column that checks the STI by doing

 class Company < ActiveRecord::Base self.inheritance_column = :company_type end 

But he is still looking for this column containing "Client". You can override what it is looking for by doing this:

 class Client < Company def self.sti_name 1 end end 

You will now see the company_type column for rows with a value of 1.

For Rails-4.2, this is almost identical, but does not require a class method:

  private self.inheritance_column = :company_type def sti_name 1 end 
+2
source

Source: https://habr.com/ru/post/1396532/


All Articles