Skip to content

Latest commit

 

History

History
80 lines (57 loc) · 1.81 KB

README.md

File metadata and controls

80 lines (57 loc) · 1.81 KB

trimodel

A rails helper gem that makes life easier when you have to handle relationships between three models. It has its own generator in order to simplify things as much as possbible.

It will generate the needed migrations between the three given models, create their associations is a separate file (keeps your code clean) and run the migrations as well.

In case you change your mind it will rollback everything without leaving any trash in your app.

In contrast to using the has_many :through association, it doesn't affect the existing database tables. Thus, regard it more as an experiment.

Setup

To install in your ruby installation, type:

  gem install trimodel

For Rails projects, add the following line in your Gemfile, inside the :development group of gems:

  group :development do
    gem 'trimodel'
  end

Type

  rails g

in your terminal and you should see the gem's generators

  Trimodel:
    trimodel:delete
    trimodel:new

Use

The gem contains two generators that create and delete the associations, the migrations and the join tables between the three given models.

For example for the models Car, Assembly and Part and wanting to find all Parts used in a Car or all Cars that use a specific Part with the Assembly as the intermediate model, type:

  trimodel:new --models Car Assembly Part

Note: The order you type the models is important.

And in your code you get all the Parts by typing

  car = Car.where(:id => 1)
  car.parts # => [Part, Part, ...] 

  part = Part.where(:id => 100)
  part.cars # => [Car, Car, ...]

Remove an association

Simply use the delete generator with the models you want to remove the association from.

  trimodel:delete --models Car Assembly Part