简体   繁体   中英

rails active record nested has many association

I have come across a DB design problem, Could one some points me the right direction.

I have has_many through the relation between Vehicle, Cylinders, InventoryItems.

The cylinder has many requirements. so far everything works well.

class Vehicle < ApplicationRecord
  has_many :inventory_items
  has_many :cylinders, through: :inventory_items
end

class InventoryItem < ApplicationRecord
  belongs_to :vehicle
  belongs_to :cylinder
end

class Cylinder < ApplicationRecord
  has_many :inventory_items
  has_many :vehicles, through: :inventory_items
  has_many :requirements
end

current sample data

Vehicle

id vehicle_type_id year  make   model
1  3               1999  honda  acccord
2  3               2017  toyota corolla
3  3               2010  ford   fiesta

Cylinder

id name 
1  v2
2  v4
3  v6
4  v8

InvetoryItem

id vehicle_id cylinder_id

1  1          2
2  1          3
3  2          1
4  2          2
5  2          3
6  2          4
7  3          2 

Requirement

id cylinder_id item_id
1  1           1000            
2  2           600 
3  3           451
4  4           550 

but I would like to change this as the requirement of cylinder changes based on the vehicle. should cylinder.requirements value depends on the vehicle value.

eg.

in vehicle - car context

Cylinder.find(1).requirements is item#1000 in vehicle - van context

Cylinder.find(1).requirements is item#189

Requirement.new( :cylynder_id => 1, :vehicle_id=> 1, :item_id => 1000 ).save

Requirement.new( :cylynder_id => 1, :vehicle_id=> 2 :item_id => 189 ).save how could I solve this problem, Please help me out?

I'm sure this is not a new challenge, but having being faced with same situation before, the two options you have in many-to-many relationship association is to either use 1. has_and_belongs_to_many when you don't need a direct relationship between two models and don't have anything else to do with the joining model 2. has_many_through when you need to do other things with the relationship model such as validation, callbacks, adding extra attributes, etc. In your case, it's looking like cylinder has many requirements through vehicle. and you might need to validate your requirements model. So another has_many through relationships between cylinder, requirements and vehicle would be what I would do. Getting complicated but it is what it is - my two cents!

First of all by naming convention of rails many_to_many association intermediate table name should be

CylinderVehicle

Quote

Unless the name of the join table is explicitly specified by using the :join_table option, Active Record creates the name by using the lexical order of the class names. So a join between Cylinder and Vehicle models will give the default join table name of “CylinderVehicle” because “C” outranks “V” in lexical ordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM