Simonas Simonas - 2 months ago 20
Ruby Question

`rails g migration add_user_id_to_posts user_id:references` creates user_id_id column

I have two models: User and Post.
In order to make an association between them I type this into the console:

rails generate migration add_user_id_to_posts user_id:references


This creates migration:

def change
add_reference :posts, :user_id, foreign_key: true
end


However, when I look into the database, it shows that I created 'user_id_id' column and not 'user_id' which I expected. What mistake am I making?

Answer
def change
  add_reference :posts, :user, foreign_key: true
end
Comments