Skip to content

Instantly share code, notes, and snippets.

@ericalli
Created May 24, 2020 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericalli/a6a530b95265477ee86e8c0a9e9f1517 to your computer and use it in GitHub Desktop.
Save ericalli/a6a530b95265477ee86e8c0a9e9f1517 to your computer and use it in GitHub Desktop.
Adding User Association to Any Table
rails generate migration AddUserRefToProducts user:references
class AddUserRefToProducts < ActiveRecord::Migration[6.0]
def change
add_reference :products, :user, foreign_key: true
end
end
# MyApp/app/controllers/products_controller.rb
#add after permit
.merge(user_id: current_user.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment