'Rails has_many association methods missing
I recently started learning rails and going through a tutorial using has_many associations but I'm running into all manners of missing method errors.
I have the following models
class Team < ApplicationRecord
has_many :members
end
class Member < ApplicationRecord
belongs_to :team
end
According to Active Record Associations
When you declare a has_many association, the declaring class automatically gains 17 methods related to the association
So I should be able to get member data when I use Team.members but I get the following error:
irb(main):004:0> Team.members
/home/jia/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-
7.0.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `members'
for Team:Class (NoMethodError)
irb(main):005:0>
irb(main):011:0> Team.size
/home/jia/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-
7.0.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `size'
for Team:Class (NoMethodError)
irb(main):012:0>
Upon checking Team.methods I don't see the 17 association methods. What am I missing or doing wrong?
I'm using Rails 7.0.1 and ruby 3.0.3
For now I'm using includes to connect the two tables but I would like to be able to use the association methods.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
