Skip to content

Commit

Permalink
Fix association documentation
Browse files Browse the repository at this point in the history
The object inside the scope is of the class that define the association
not the associated class.

[ci skip]
  • Loading branch information
rafaelfranca committed May 16, 2017
1 parent ff55c6e commit 0714251
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -1276,7 +1276,7 @@ module ClassMethods
# Scope examples:
# has_many :comments, -> { where(author_id: 1) }
# has_many :employees, -> { joins(:address) }
# has_many :posts, ->(post) { where("max_post_length > ?", post.length) }
# has_many :posts, ->(blog) { where("max_post_length > ?", blog.max_post_length) }
#
# === Extensions
#
Expand Down Expand Up @@ -1443,7 +1443,7 @@ def has_many(name, scope = nil, options = {}, &extension)
# Scope examples:
# has_one :author, -> { where(comment_id: 1) }
# has_one :employer, -> { joins(:company) }
# has_one :dob, ->(dob) { where("Date.new(2000, 01, 01) > ?", dob) }
# has_one :latest_post, ->(blog) { where("created_at > ?", blog.enabled_at) }

This comment has been minimized.

Copy link
@ClayShentrup

ClayShentrup Nov 8, 2021

@rafaelfranca Doesn't this need class_name: 'Post'?

#
# === Options
#
Expand Down Expand Up @@ -1573,7 +1573,7 @@ def has_one(name, scope = nil, options = {})
# Scope examples:
# belongs_to :firm, -> { where(id: 2) }
# belongs_to :user, -> { joins(:friends) }
# belongs_to :level, ->(level) { where("game_level > ?", level.current) }
# belongs_to :level, ->(game) { where("game_level > ?", game.current_level) }
#
# === Options
#
Expand Down Expand Up @@ -1769,9 +1769,8 @@ def belongs_to(name, scope = nil, options = {})
#
# Scope examples:
# has_and_belongs_to_many :projects, -> { includes(:milestones, :manager) }
# has_and_belongs_to_many :categories, ->(category) {
# where("default_category = ?", category.name)
# }
# has_and_belongs_to_many :categories, ->(post) {
# where("default_category = ?", post.default_category)
#
# === Extensions
#
Expand Down

0 comments on commit 0714251

Please sign in to comment.