# Has\_many working correctly only on reload!

**URL:** <https://www.ruby-forum.com/t/has-many-working-correctly-only-on-reload/97080>\
**Category:** Rails\
**Created:** [July 4, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/has-many-working-correctly-only-on-reload/97080 "2006-07-04T20:08:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [July 4, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/has-many-working-correctly-only-on-reload/97080/1 "2006-07-04T20:08:00Z")

</div>

I have this code that is using svn externals with rails EDGE working  
fine for the past couple of months. A couple of days ago, this code  
died. Even after deleting vendor/rails, this code doesnt work:

class Tag \< ActiveRecord::Base  
has\_many :taggings  
has\_many :events, :through =\> :taggings  
has\_many :users, :through =\> :taggings  
end

class User \< ActiveRecord::Base

# Virtual attribute for the unencrypted password

attr\_accessor :password  
has\_many :events  
has\_many :posts  
has\_many :taggings  
has\_many :tags, :through =\> :taggings  
end

class Event \< ActiveRecord::Base

acts\_as\_versioned do  
def self.included(base)  
base.belongs\_to :user  
base.belongs\_to :region  
end  
end  
has\_many :taggings  
has\_many :tags, :through =\> :taggings  
has\_many :posts  
end

> > Tag.find(10).users  
> > NoMethodError: undefined method `users' for #<Tag:0x26f4d08 @attributes={"name"=>"rails", "id"=>"10"}> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in`method\_missing’  
> > from (irb):1
> 
> > Tag.find(10).events  
> > NoMethodError: undefined method `events' for #<Tag:0x26f16e4 @attributes={"name"=>"rails", "id"=>"10"}> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in`method\_missing’  
> > from (irb):2
> 
> > reload!  
> > Reloading…  
> > =\> [ApplicationController, Tagging, Tag]
> 
> > Tag.find(10).events  
> > #works fine now
> 
> > Tag.find(10).users  
> > #works fine now

WTF!??!?!?!

Why can’t Tag use the associations?

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [July 5, 2006, 5:38am UTC](https://www.ruby-forum.com/t/has-many-working-correctly-only-on-reload/97080/2 "2006-07-05T05:38:07Z")

</div>

Jason Toy wrote:

> I have this code that is using svn externals with rails EDGE working  
> fine for the past couple of months. A couple of days ago, this code  
> died. Even after deleting vendor/rails, this code doesnt work:
> 
> class Tag \< ActiveRecord::Base  
> has\_many :taggings  
> has\_many :events, :through =\> :taggings  
> has\_many :users, :through =\> :taggings  
> end
> 
> class User \< ActiveRecord::Base
> 
> # Virtual attribute for the unencrypted password
> 
> attr\_accessor :password  
> has\_many :events  
> has\_many :posts  
> has\_many :taggings  
> has\_many :tags, :through =\> :taggings  
> end
> 
> class Event \< ActiveRecord::Base
> 
> acts\_as\_versioned do  
> def self.included(base)  
> base.belongs\_to :user  
> base.belongs\_to :region  
> end  
> end  
> has\_many :taggings  
> has\_many :tags, :through =\> :taggings  
> has\_many :posts  
> end
> 
> > > Tag.find(10).users  
> > > NoMethodError: undefined method `users' for #<Tag:0x26f4d08 @attributes={"name"=>"rails", "id"=>"10"}> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in `method\_missing’  
> > > from (irb):1  
> > > Tag.find(10).events  
> > > NoMethodError: undefined method `events' for #<Tag:0x26f16e4 @attributes={"name"=>"rails", "id"=>"10"}> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in `method\_missing’  
> > > from (irb):2  
> > > reload!  
> > > Reloading…  
> > > =\> [ApplicationController, Tagging, Tag]  
> > > Tag.find(10).events  
> > > #works fine now  
> > > Tag.find(10).users  
> > > #works fine now
> 
> WTF!??!?!?!
> 
> Why can’t Tag use the associations?

You don’t have any belongs\_to associations anywhere. Do you have foreign  
keys in your taggings table? It looks like you put together your models  
(and I suspect your schema as well) without really understanding how  
associations are supposed to work. Take a look at some of the examples  
on my blog, and read up on how this stuff works. These articles in  
particular should be helpful:

[http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off)  
[http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness](http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness)  
[http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2](http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2)  
[http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through](http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through)

Good luck!

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [July 11, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/has-many-working-correctly-only-on-reload/97080/3 "2006-07-11T17:30:58Z")

</div>

Hi Josh, thanks for replying. I forgot to post the taggable table, so  
it seems like I have the correct relation. I had double checked your  
articles before, and I checked them again, they se accurate, I am sitll  
not sure what is wrong, do you have an idea? Thanks.  
I have foreign keys on the Taggings table:  
class Tagging \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :tag  
belongs\_to :event  
end

Josh S. wrote:

> Jason Toy wrote:
> 
> > I have this code that is using svn externals with rails EDGE working  
> > fine for the past couple of months. A couple of days ago, this code  
> > died. Even after deleting vendor/rails, this code doesnt work:
> > 
> > class Tag \< ActiveRecord::Base  
> > has\_many :taggings  
> > has\_many :events, :through =\> :taggings  
> > has\_many :users, :through =\> :taggings  
> > end
> > 
> > class User \< ActiveRecord::Base
> > 
> > # Virtual attribute for the unencrypted password
> > 
> > attr\_accessor :password  
> > has\_many :events  
> > has\_many :posts  
> > has\_many :taggings  
> > has\_many :tags, :through =\> :taggings  
> > end
> > 
> > class Event \< ActiveRecord::Base
> > 
> > acts\_as\_versioned do  
> > def self.included(base)  
> > base.belongs\_to :user  
> > base.belongs\_to :region  
> > end  
> > end  
> > has\_many :taggings  
> > has\_many :tags, :through =\> :taggings  
> > has\_many :posts  
> > end
> > 
> > > > Tag.find(10).users  
> > > > NoMethodError: undefined method `users' for #<Tag:0x26f4d08 @attributes={"name"=>"rails", "id"=>"10"}> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in `method\_missing’  
> > > > from (irb):1  
> > > > Tag.find(10).events  
> > > > NoMethodError: undefined method `events' for #<Tag:0x26f16e4 @attributes={"name"=>"rails", "id"=>"10"}> from /opt/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1792:in `method\_missing’  
> > > > from (irb):2  
> > > > reload!  
> > > > Reloading…  
> > > > =\> [ApplicationController, Tagging, Tag]  
> > > > Tag.find(10).events  
> > > > #works fine now  
> > > > Tag.find(10).users  
> > > > #works fine now
> > 
> > WTF!??!?!?!
> > 
> > Why can’t Tag use the associations?
> 
> You don’t have any belongs\_to associations anywhere. Do you have foreign  
> keys in your taggings table? It looks like you put together your models  
> (and I suspect your schema as well) without really understanding how  
> associations are supposed to work. Take a look at some of the examples  
> on my blog, and read up on how this stuff works. These articles in  
> particular should be helpful:
> 
> [http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off](http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off)  
> [http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness](http://blog.hasmanythrough.com/articles/2006/02/28/association-goodness)  
> [http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2](http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2)  
> [http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through](http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through)
> 
> Good luck!
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
