has_many :through problem

This may be a stupid question but I seriously can not find what's wrong. Using Rails 2.3 and Ruby 1.8.7 I have:

class Category < ActiveRecord::Base   has_many :categories_users   has_many :users, :through => :categories_users %I skip other things here end

class User < ActiveRecord::Base   has_many :categories_users   has_many :categories, :through => :categories_users end

class CategoryUser < ActiveRecord::Base   validates_presence_of :category_id, :user_id   belongs_to :category   belongs_to :user end

and the migration was:

  def self.up      drop_table :categories_users     create_table :categories_users do |t|       t.integer :category_id, :user_id       t.timestamps     end   end

I added the timestamps out of desperation, dont think they are needed. I really can not see what's wrong with this set up but:

$ script/console Loading development environment (Rails 2.3.2)

a =Category.new

/home/jordi/sources/estudio/app/models/category.rb:60: warning: don't put space before argument parentheses => #<Category id: nil, name: nil, created_at: nil, updated_at: nil, public: false, created_by: 1>

a.users

NameError: uninitialized constant Category::CategoriesUser         from /home/jordi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/ active_support/dependencies.rb:105:in `const_missing'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/base.rb:2204:in `compute_type'         from /home/jordi/.gem/ruby/1.8/gems/activesupport-2.3.2/lib/ active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/base.rb:2200:in `compute_type'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/reflection.rb:156:in `send'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/reflection.rb:156:in `klass'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/reflection.rb:257:in `source_reflection'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/reflection.rb:257:in `collect'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/reflection.rb:257:in `source_reflection'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/reflection.rb:288:in `check_validity!'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/associations/has_many_through_association.rb:5:in `initialize'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/associations.rb:1277:in `new'         from /home/jordi/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/associations.rb:1277:in `users'         from (irb):2

I have being like 2 days trying to solve this, I am sure is some evident and stupid thing. Any hint will be very welcomed.

This may be a stupid question but I seriously can not find what's wrong. Using Rails 2.3 and Ruby 1.8.7 I have:

class Category < ActiveRecord::Base has_many :categories_users

rails will singularise this and try and look for a class caller CategoriesUser (as you can tell from the error message)

class CategoryUser < ActiveRecord::Base

but your class is called CategoryUser.

Fred

Indeed that was the problem. Thank you very much.

Hi Andrew,

could you please try this - class Bookmark has_many :bookmarks_tags has_many :tags, :through => :bookmarks_tags .

class BookmarksTag belongs_to :tag belongs_to :bookmark ..

Then @bookmark.tags should work.

Thanks