Home Rails 4.2: Turn a reference field into foreign key
Reply: 0

Rails 4.2: Turn a reference field into foreign key

user781
1#
user781 Published in April 22, 2018, 12:31 am

I'm maintaining an old system that uses Rails 4.2 and for an unknown reason the references are created this way:

  t.references :credit_card, null: false
  t.references :car, null: false
  t.references :profile, null: false

This allows me to create registers for this model with invalid credit_card ids, for example. Foreign key isn't validated.

Generating index migration didn't turn them into fk's and didn't validate them at all:

class AddIndexToRentals < ActiveRecord::Migration
  def change
    add_index :rentals, :credit_card_id
    add_index :rentals, :car_id
    add_index :rentals, :profile_id
  end
end

How can I make those fields foreign keys and accept only existent ids?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317317 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO