ruby on rails - How do I establish a relationship between two objects where both need to have many of the other object? -


my situation this: have 2 models, model (as in car model) , engine. have models have more 1 engine (different model years came different engines), , have engines belong multiple different models (single engine reused across multiple models).

forgive me being (very) new rails , activerecord, seems bit more complicated has_many , belongs_to. wrong. should note i'm using rails 5.

given have scaffolds/models in place , i'd rather not delete them, how write migration achieve above situation? need add respective models?

use many-to-many relationship, make sure migration name contains jointable

rails g migration createenginemodeljointable engines models 

engine class

class engine < activerecord::base   has_and_belongs_to_many :models end 

model class

class model < activerecord::base   has_and_belongs_to_many :engines end 

you can access by

engines = model.engines models = engine.models 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -