# Has\_many :through problem

**URL:** <https://www.ruby-forum.com/t/has-many-through-problem/166356>\
**Category:** Rails\
**Created:** [April 24, 2009, 9:43am UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356 "2009-04-24T09:43:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2009, 9:43am UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356/1 "2009-04-24T09:43:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2009, 9:53am UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356/2 "2009-04-24T09:53:06Z")

</div>

Indeed that was the problem. Thank you very much.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 24, 2009, 9:44am UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356/3 "2009-04-24T09:44:54Z")

</div>

On Apr 24, 5:54 am, jordi polo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 21, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356/4 "2009-05-21T20:48:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 24, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356/5 "2009-05-24T13:44:12Z")

</div>

Hi Manasi,

This worked. Thank you!

Another thing I found I had to do was rename BookmarksTag.rb to  
bookmarks\_tag.rb. As a Java programmer who is just starting in ruby I  
am still coming to grips with the ruby naming conventions.

Your help was much appreciated. Thank you again!

Andrew.

Manasi V1 wrote:

> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 20, 2009, 4:25am UTC](https://www.ruby-forum.com/t/has-many-through-problem/166356/6 "2009-05-20T04:25:47Z")

</div>

I have what I thought was a similar problem, but I am not sure what’s  
going wrong.

Here’s my model:

class Bookmark \< ActiveRecord::Base  
has\_many :tags, :through =\> :bookmarks\_tags  
end

(in file app/models/bookmark.rb)

class Tag \< ActiveRecord::Base  
has\_many\_and\_belongs\_to\_many :bookmarks, :through =\> :bookmarks\_tags  
end

(in file app/models/tag.rb)

class BookmarksTag \< ActiveRecord::Base  
belongs\_to :bookmark  
belongs\_to :tag  
end

(in file app/models/BookmarksTag.rb)

My database looks like this:

CREATE TABLE `bookmarks` (  
`id` int(11) NOT NULL auto\_increment,  
`name` varchar(255) default NULL,  
`url` varchar(255) default NULL,  
`created_at` datetime default NULL,  
`updated_at` datetime default NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO\_INCREMENT=15 ;

* * *

## – – Table structure for table `bookmarks_tags`

CREATE TABLE `bookmarks_tags` (  
`bookmark_id` int(11) default NULL,  
`tag_id` int(11) default NULL,  
`created_at` datetime default NULL,  
`updated_at` datetime default NULL  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

* * *

## – – Table structure for table `schema_migrations`

CREATE TABLE `schema_migrations` (  
`version` varchar(255) NOT NULL,  
UNIQUE KEY `unique_schema_migrations` (`version`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

* * *

## – – Table structure for table `tags`

CREATE TABLE `tags` (  
`id` int(11) NOT NULL auto\_increment,  
`tagword` varchar(255) default NULL,  
`created_at` datetime default NULL,  
`updated_at` datetime default NULL,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO\_INCREMENT=4 ;

I’m getting this error in my “show” view:

Could not find the association :bookmarks\_tags in model Bookmark

Extracted source (around line #13):

10:  
11:

12: **Tags:**  
13: \<% for tag in @bookmark.tags %\>  
14: \<%= tag.tagword %\>  
15: \<% end %\>  
16:

If I change bookmark.rb as follows it works, but I would like to learn  
what I’ve done wrong for future reference.

class Bookmark \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :tags  
end

Any advice would be greatly appreciated.

Regards,  
Andrew.
