0

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.

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103

1 Answers1

0

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
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • I appreciate your answer. However, I need to know if I can avoid manual editing migration files or writing some workaround. –  May 22 '17 at 15:13
  • 1
    @ar7max I think I have answered that. Your relations are "unconventional", and so you get less hand-holding from rails. – Sergio Tulentsev May 22 '17 at 16:39