Has_many working correctly only on reload!

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:inmethod_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:inmethod_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?

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:inmethod_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:inmethod_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/02/28/association-goodness
http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2
http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through

Good luck!


Josh S.
http://blog.hasmanythrough.com

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:inmethod_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:inmethod_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/02/28/association-goodness
http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2
http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through

Good luck!


Josh S.
http://blog.hasmanythrough.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs