Ruby on Rails - belongs to in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
A belongs_to association sets up a one-to-one connection with another model, so each instance of the declaring model "belongs to" one instance of the other model.
Example
- Our application includes users and posts, and each post can be assigned to exactly one user,
- We'd declare the post model this way:
class Post < ApplicationRecord
belongs_to :user
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Output
In our table structure we might then have
create_table "posts", force: :cascade do |t|
t.integer "user_id", limit: 4
end