Both will generate the same columns when you run the migration. In rails console, you can see that this is the case:
:001 > Micropost => Micropost(id: integer, user_id: integer, created_at: datetime, updated_at: datetime)
The second command adds a belongs_to :user
relationship in your Micropost model whereas the first does not. When this relationship is specified, ActiveRecord will assume that the foreign key is kept in the user_id
column and it will use a model named User
to instantiate the specific user.
The second command also adds an index on the new user_id
column.
Related Posts:
- Nginx error: client intended to send too large body
- where is devise implementation of “authenticate_user!” method?
- What is the difference between “rake db:seed” and rake db:fixtures:load”
- How do I parse JSON with Ruby on Rails? [duplicate]
- 400 Bad Request – request header or cookie too large
- Advanced AREL or just Rails Query for has_many through search by association
- Why Puma listen on ‘tcp://localhost:3000’ instead of ‘http://localhost:3000’
- Explain what " means
- Why does Ruby on Rails use http://0.0.0.0:3000 instead of http://localhost:3000?
- ActionController::InvalidAuthenticityToken
- bundle install returns “Could not locate Gemfile”
- ActionController::InvalidAuthenticityToken
- AngularJS- Login and Authentication in each route and controller
- Rails: Why “sudo” command is not recognized?
- Rails 5 ActionController::InvalidAuthenticityToken error
- what does ? ? mean in ruby
- Rails: Can’t verify CSRF token authenticity when making a POST request
- Getting error: Peer authentication failed for user “postgres”, when trying to get pgsql working with rails
- undefined method `each’ for nil:NilClass… why?
- Bundler: You must use Bundler 2 or greater with this lockfile
- undefined method `each’ for nil:NilClass… why?
- Using Pundit for all-access “super_admin” role
- How to tell if homebrew is installed on Mac OS X
- Parsing XML with Ruby
- What is the difference between Rails.cache.clear and rake tmp:cache:clear?
- Unable to install gem – Failed to build gem native extension – cannot load such file — mkmf (LoadError)
- Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib
- Cannot load such file — bcrypt_ext
- How do I remove Permission denied @ rb_sysopen – Gem install error?
- Email validation in Ruby on Rails?
- Can’t find the ‘libpq-fe.h header when trying to install pg gem
- warning: constant ::Fixnum is deprecated When generating new model
- could not connect to server: No such file or directory (PG::ConnectionBad)
- PG::ConnectionBad: fe_sendauth: no password supplied
- curl : (1) Protocol https not supported or disabled in libcurl
- extconf failed, exit code 1 error when installing [Ruby on Rails] 5 on OS X Yosemite 10.10.5
- Rails 4 RoutingError: No Route Matches
- couldn’t find file ‘jquery’ with type ‘application/javascript’
- Passing parameters in rails redirect_to
- Getting: “Migrations are pending; run ‘bin/rake db:migrate RAILS_ENV=development’ to resolve this issue.” after cloning and migrating the project
- PG::ConnectionBad – could not connect to server: Connection refused
- Rails syntax error : unexpected keyword_ensure, expecting end-of-input
- How to find out which rails version an existing rails application is built on?
- RubyMine Unit tests – Test Framework quit unexpectedly
- bcrypt LoadError: Cannot load such file
- Uninitialized constant “Controller Name”
- Devise lockable – How to unlock account using unlock_in
- Ruby: What is the easiest way to remove the first element from an array?
- How do I format datetime in rails?
- Ruby String to Date Conversion
- heroku open – no app specified
- Difference between rake db:migrate db:reset and db:schema:load
- “Error installing rails” because “extconf.rb failed” on Ubuntu 18.04
- TypeError: no implicit conversion of Symbol into Integer
- Sass::SyntaxError: File to import not found or unreadable: bootstrap-sprockets
- An unhandled lowlevel error occurred. The application logs may have details
- Uncaught ReferenceError: React is not defined
- Rails button_to vs. HTML Button Tag
- GROUP BY and COUNT using ActiveRecord
- How to stop (and restart) the Rails Server?
- Purge or recreate a Ruby on Rails database
- Migrations are pending; run ‘bin/rake db:migrate RAILS_ENV=development’ to resolve this issue
- Uploading a file in Rails
- An error occurred while installing json (1.8.1), and Bundler cannot continue – Rails
- How to force or redirect to SSL in nginx?