has many

suggest change

A has_many association indicates a one-to-many connection with another model. This association generally is located on the other side of a belongs_to association.

This association indicates that each instance of the model has zero or more instances of another model.

For example, in an application containing users and posts, the user model could be declared like this:

class User < ApplicationRecord
  has_many :posts
end

The table structure of Post would remain the same as in the belongs_to example; in contrast, User would not require any schema changes.

If you want to get the list of all the published posts for the User, then you can add the following (i.e. you can add scopes to your association objects):

class User < ApplicationRecord
  has_many :published_posts, -> { where("posts.published IS TRUE") }, class_name: "Post"
end

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ActiveRecord Associations:
*has many

Table Of Contents
10ActiveRecord Associations
55CSV