3

I've created a join table with

rails generate migration CreateJoinTableFoosBars foos bars

and it has indeed created a working joining table, however I can't see the model for it and thus I can't add the

validates_uniqueness_of :foo_id, scope: :bar_id

How can I do it in this case? Thanks in advance.

1
  • why don't you create a model manually for it?
    – power
    Jul 29, 2016 at 9:17

2 Answers 2

10

Add a unique index that includes both columns. That will prevent you from inserting a record that contains a duplicate foo_id/bar_id pair.

add_index :foo_bars, [:foo_id, :bar_id], unique: true
2
  • I'm sorry for the dumb question, but I'm new to rails, I have to create a migration and add this line in it, correct?
    – Robert
    Jul 29, 2016 at 9:30
  • Yes, after you created the join table
    – siegy22
    Jul 29, 2016 at 9:32
-3

generating a migration does not generate a model as well. If you use the model generator then it will also produce a migration file and the model file:

rails generate model foo_bar foo_id:integer bar_id:integer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.