Reputation: 303
Let's say, on a rails app (with postgresql), I have this database with a polymorphic association between work_steps and sub_assemblies / parts:
Part.rb
belongs_to :sub_assembly, optional: true
has_many :work_steps, as: :component
belongs_to :site
belongs_to :car
Sub_assembly.rb
has_many :work_steps, as: :component
has_many :parts
work_step.rb
belongs_to :component, polymorphic: true
Now, I want to query my database: I want a list of all the work_steps filtered with a specific array of sites and a specific array of cars. For instance, all the work_steps linked to parts that belongs to Site A and Car X & Z.
Because of the polymorphic association with the sub_assemblies and this same table which is linked to parts table, I cannot figure out how to do it.
At the end, I need an ActiveRecord Relation. How would you do it simply? I tried a lot of things but without success.
Any one to help me?
Thank you in advance.
Upvotes: 1
Views: 432
Reputation: 1037
Okay, so work_step is what you want to be able to be used by multiple models.
So inside the migration for CreateWorkSteps:
t.references :component, polymorphic: true
This basically does
t.integer :component_id # Refers to id (of sub-assembly or part)
t.string :component_type # Refers to type of object (sub-assembly or part)
add_index :work_steps, [:component_type, component_id]
Now in your model:
work_step.rb
belongs_to :component, polymorphic: true
sub_assembly.rb
has_many :work_steps, as: :component
has_many :parts
part.rb
has_many :work_steps, as: :component
belongs_to :site
belongs_to :car
belongs_to :sub_assembly, optional: true
Okay, so you wish to query your database and obtain
So, here the list of steps you should do in ActiveRecord (I'm not going to give a single-query answer as that's too much effort. Also, the step order is in logical order, but remember that ActiveRecord queries use lazy evaluation and SQL queries are evaluated in a certain order.)
Good luck! I won't hold your hand with a pre-written answer, through...because I'm too lazy to boot into Rails, make migrations, make models, seed the models, then write the query myself. ;)
Oh ya, and I ended up giving a single query answer anyway.
Upvotes: 1