# Many to many :through problem

**URL:** <https://www.ruby-forum.com/t/many-to-many-through-problem/146724>\
**Category:** Rails\
**Created:** [August 24, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/many-to-many-through-problem/146724 "2008-08-24T19:30:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [August 24, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/many-to-many-through-problem/146724/1 "2008-08-24T19:30:03Z")

</div>

Hi all,

This should be a simple one, but I just don’t see my mistake.

Migration1:  
class CreateCompanyUsers \< ActiveRecord::Migration  
def self.up  
create\_table :employments, :id =\> false do |t|  
t.boolean :admin\_role  
t.integer :company\_id, :user\_id  
t.timestamps  
end  
end

def self.down  
drop\_table :employments  
end  
end

Migration2:  
class CreateCompanies \< ActiveRecord::Migration  
def self.up  
create\_table :companies do |t|  
t.string :name, :subdomain  
t.boolean :admin\_company  
t.timestamps  
end  
add\_index :companies, :subdomain, :unique =\> true  
Company.create :name =\> “Default”, :subdomain =\>  
“default”, :admin\_company =\> true  
end

def self.down  
drop\_table :companies  
end  
end

Migration3:  
class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table “users”, :force =\> true do |t|  
t.column :login, :string, :limit =\> 40  
t.column :name, :string, :limit =\>  
100, :default =\> ‘’, :null =\> true  
t.column :email, :string, :limit =\> 100  
t.column :crypted\_password, :string, :limit =\> 40  
t.column :salt, :string, :limit =\> 40  
t.column :created\_at, :datetime  
t.column :updated\_at, :datetime  
t.column :remember\_token, :string, :limit =\> 40  
t.column :remember\_token\_expires\_at, :datetime

```
end
add_index :users, :login, :unique => true
User.create :login => "admin", :name => "Administrator

```

God", :email =\> ‘removed\_email\_address@domain.invalid’, :password =\>  
‘password’, :password\_confirmation =\> ‘password’  
end

def self.down  
drop\_table “users”  
end  
end

And my models:  
class Employment \< ActiveRecord::Base  
belongs\_to :company  
belongs\_to :user  
end

class Company \< ActiveRecord::Base  
has\_many :employments  
has\_many :users, :through =\> :employments  
end

class User \< ActiveRecord::Base

# cut out restful\_auth stuff

has\_many :employments  
has\_many :companies, :through =\> :employments

When testing in script/console:  
Loading development environment (Rails 2.1.0)

> > user=User.find :first  
> > =\> #\<User id: 1, login: “admin”, name: “Administrator God”, email:  
> > “removed\_email\_address@domain.invalid”, crypted\_password:  
> > “a5d460ff8566a5e0e11b6e591f1d8921a5726ea6”, salt:  
> > “872d3a4ebb1d9b2ef6be18938d36aea3bafe2646”, created\_at: “2008-08-24  
> > 15:53:40”, updated\_at: “2008-08-24 15:53:40”, remember\_token: nil,  
> > remember\_token\_expires\_at: nil\>
> 
> > company=Company.find :first  
> > =\> #\<Company id: 1, name: “Default”, subdomain: “default”,  
> > admin\_company: true, created\_at: “2008-08-24 15:53:40”, updated\_at:  
> > “2008-08-24 15:53:40”\>
> 
> > company.users \<\< user  
> > NameError: uninitialized constant Company::Employment  
> > from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/  
> > active\_support/dependencies.rb:492:in `const_missing' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:1909:in`compute\_type’  
> > from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
> > reflection.rb:129:in `send' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ reflection.rb:129:in`klass’  
> > from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
> > reflection.rb:189:in `source_reflection' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ reflection.rb:189:in`collect’  
> > from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
> > reflection.rb:189:in `source_reflection' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ reflection.rb:198:in`check\_validity!’  
> > from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
> > associations/has\_many\_through\_association.rb:5:in `initialize' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ associations.rb:1128:in`new’  
> > from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
> > associations.rb:1128:in `users’  
> > from (irb):3

So what did I do wrong, or what did I miss?

Kind regards,  
Wijnand

---

<div class="post-metadata">

**Author:** ![Wijnand\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a9adbd/32.png) [@Wijnand\_W](https://www.ruby-forum.com/u/Wijnand_W)\
**Post date:** [August 24, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/many-to-many-through-problem/146724/2 "2008-08-24T19:39:27Z")

</div>

On 24 Aug 2008, at 17:12, Wijnand W. wrote:

> ```
> t.integer :company_id, :user_id
> t.timestamps
> 
> ```
> 
> end  
> end

With has many through, the join table is a model in its own right so  
you must not skip :id like you are doing here.

> When testing in script/console:  
> admin\_company: true, created\_at: “2008-08-24 15:53:40”, updated\_at:  
> “2008-08-24 15:53:40”\>
> 
> > > company.users \<\< user  
> > > NameError: uninitialized constant Company::Employment

That’s odd, it couldn’t find your Employment class. Is it in  
employment.rb ?

Fred
