All you have to do is to replace
puts aaa.bbbs(:bbb_a)
With either
puts aaa.bbbs.first
Or
puts aaa.bbbs[0]
Because you have a has_many relationship between Aaa
and Bbb
models, rails creates dynamic instance methods for you as Aaa#bbbs
and Bbb#aaa
.
This method accept 0 or 1 arguments. If you pass an argument to this method as true
then it fires the query on the database to retrieve the associated records. And If you pass an argument to this method as false
then it just picks up the cached results of the query (provided query was fired once before for the caching to take place).
So, when you do
puts aaa.bbbs(:bbb_a)
The value passed is simply ignored as its neither true
or false
.