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

taylorthurlow

My situation is this: I have two models, Model (as in car model) and Engine. I have some Models which have more than one Engine (different model years came with different engines), and I have some Engines which belong to multiple different Models (single engine was reused across multiple models).

Forgive me for being (very) new to Rails and ActiveRecord, but this seems a bit more complicated than just a has_many and belongs_to. I could be wrong. I should also note that I'm using Rails 5.

Given that I already have my scaffolds/models in place and I'd rather not delete them, how do I write a migration to achieve the above situation? What do I need to add to the respective models?

jamesjaya

Use many-to-many relationship, make sure your 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 it by

engines = model.engines
models = engine.models

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I have multiple has_many to has_many relationship between two models?

From Dev

Rails: What do I need to do to create a many-to-many relationship between two models within a namespace?

From Dev

Rails: What do I need to do to create a many-to-many relationship between two models within a namespace?

From Dev

How do I create a functor instance for a type with two arguments, where both arguments have to be the same type?

From Dev

I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship?

From Dev

How to establish a strong before after relationship between two public functions

From Dev

How to establish a strong before after relationship between two public functions

From Dev

Name of a data relationship where an object cannot have both parent and children?

From Dev

How do I bulk insert two datatables that have an Identity relationship

From Dev

How do I structure a SQL query to find an object that is the parent of two specific other objects?

From Dev

I have two units and a class in each unit that need each other to function. How do I avoid a circular reference?

From Dev

I have two lists containing the same objects. How do I change one list without changing the other?

From Dev

Grails: How do I fetch a sorted list of domain objects in a many-to-many relationship with sort attribute?

From Dev

MagicalRecord - ManyToMany: Do I need to add entities on both sides of the relationship?

From Dev

Do I need to make one-to-many relationship to simple model?

From Dev

I have a ul with 5 li i and two other li in both side pre and next i wanna do as below

From Dev

How do I write a this constructor to establish or or the other possibility?

From Dev

How do I remove/replace objects in an ordered to-many CoreData relationship?

From Dev

Angular 2, share an object between two components that have no direct relationship

From Dev

Angular 2, share an object between two components that have no direct relationship

From Dev

How do I add a node between two other nodes in a HBox?

From Dev

How do I differentiate between two json objects with same key?

From Dev

How to query two collections that have a "one-to-many" relationship in MongoDB?

From Dev

php mysql If I do not have the same data between two tables, can I print the other table data?

From Dev

I have a practice project i did that i need to compare two Lists . How do i make the comparison?

From Dev

How do I connect two objects with a method to return a new object

From Dev

How many of these wrapped Java I/O objects do I have to close?

From Dev

How do I add a relationship between two charms to pass information between them?

From Dev

validateForInsert fails when I set relationship between two valid objects

Related Related

  1. 1

    How do I have multiple has_many to has_many relationship between two models?

  2. 2

    Rails: What do I need to do to create a many-to-many relationship between two models within a namespace?

  3. 3

    Rails: What do I need to do to create a many-to-many relationship between two models within a namespace?

  4. 4

    How do I create a functor instance for a type with two arguments, where both arguments have to be the same type?

  5. 5

    I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship?

  6. 6

    How to establish a strong before after relationship between two public functions

  7. 7

    How to establish a strong before after relationship between two public functions

  8. 8

    Name of a data relationship where an object cannot have both parent and children?

  9. 9

    How do I bulk insert two datatables that have an Identity relationship

  10. 10

    How do I structure a SQL query to find an object that is the parent of two specific other objects?

  11. 11

    I have two units and a class in each unit that need each other to function. How do I avoid a circular reference?

  12. 12

    I have two lists containing the same objects. How do I change one list without changing the other?

  13. 13

    Grails: How do I fetch a sorted list of domain objects in a many-to-many relationship with sort attribute?

  14. 14

    MagicalRecord - ManyToMany: Do I need to add entities on both sides of the relationship?

  15. 15

    Do I need to make one-to-many relationship to simple model?

  16. 16

    I have a ul with 5 li i and two other li in both side pre and next i wanna do as below

  17. 17

    How do I write a this constructor to establish or or the other possibility?

  18. 18

    How do I remove/replace objects in an ordered to-many CoreData relationship?

  19. 19

    Angular 2, share an object between two components that have no direct relationship

  20. 20

    Angular 2, share an object between two components that have no direct relationship

  21. 21

    How do I add a node between two other nodes in a HBox?

  22. 22

    How do I differentiate between two json objects with same key?

  23. 23

    How to query two collections that have a "one-to-many" relationship in MongoDB?

  24. 24

    php mysql If I do not have the same data between two tables, can I print the other table data?

  25. 25

    I have a practice project i did that i need to compare two Lists . How do i make the comparison?

  26. 26

    How do I connect two objects with a method to return a new object

  27. 27

    How many of these wrapped Java I/O objects do I have to close?

  28. 28

    How do I add a relationship between two charms to pass information between them?

  29. 29

    validateForInsert fails when I set relationship between two valid objects

HotTag

Archive