Define reference name

49 views Asked by At

Is there a way to define reference name with "rails generate" command?

There is table A with two fields - a1 and a2: references to table B.

1

There are 1 answers

2
Sergio Tulentsev On BEST ANSWER

One of the main principles of rails is "convention over configuration". It works great for cases like this:

 rails generate model Player team:references

This will create column team_id and mark it as foreign key to table teams.

This covers 90% of all possible needs (educated guesstimate). If you have a "standard" model, you'll generate the boilerplate for it in seconds. But if you have a more exotic case (like the one in the question), then you'll have to do a bit of manual work and touch migration file and model relation definitions yourself. Which is quite easy as well.

t.references :a1, references: :b
t.references :a2, references: :b