# Many to many, error on relation

**URL:** <https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208>\
**Category:** Rails\
**Created:** [February 20, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208 "2007-02-20T17:36:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![surfman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@surfman](https://www.ruby-forum.com/u/surfman)\
**Post date:** [February 20, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208/1 "2007-02-20T17:36:21Z")

</div>

two tables, users and languages,

one user may have many languages, one language may belong to many users.  
so, I create the joint table users\_languages with user\_id and  
language\_id only.

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :languages  
end

class Language \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

class UsersLanguages \< ActiveRecord::Base  
belongs\_to :users  
belongs\_to :languages  
end

I also create one sample user data into users tables and one sample  
language data into languages table. nothing in joint table  
users\_languages.

then, I go to console to test:

if I use the following code, everything is fine.

l=Language.new  
u=User.new  
u.languages \<\< l

but when I use the real data, still under console,

l=Language.find(1)  
u=User.find(1)  
u.languages \<\< l

when I am at the third line, I will get an error message like this:

ActiveRecord::StatementInvalid: PGError: ERROR: relation  
“languages\_users” does not exist  
: SELECT a.attname, format\_type(a.atttypid, a.atttypmod),  
d.adsrc, a.attnotnull  
FROM pg\_attribute a LEFT JOIN pg\_attrdef d  
ON a.attrelid = d.adrelid AND a.attnum = d.adnum  
WHERE a.attrelid = ‘languages\_users’::regclass  
AND a.attnum \> 0 AND NOT a.attisdropped  
ORDER BY a.attnum

any reason for that? thanks for any help.

---

<div class="post-metadata">

**Author:** ![surfman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@surfman](https://www.ruby-forum.com/u/surfman)\
**Post date:** [February 20, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208/2 "2007-02-20T17:44:50Z")

</div>

Surfman J. wrote:

> class UsersLanguages \< ActiveRecord::Base  
> belongs\_to :users  
> belongs\_to :languages  
> end

Rails expects the join table to be named languanges\_users. Also, you  
don’t want a model for this table, you simple create a migration:

class LanguagesUsers \< ActiveRecord::Migration  
def self.up  
create\_table :languages\_users, :id =\> false do |t|  
t.column :language\_id, :integer, :null =\> false  
t.column :user\_id, :integer, :null =\> false  
end  
end

def self.down  
drop\_table :languages\_users  
end  
end

---

<div class="post-metadata">

**Author:** ![surfman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@surfman](https://www.ruby-forum.com/u/surfman)\
**Post date:** [February 20, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208/3 "2007-02-20T18:04:15Z")

</div>

Drew O. wrote:

> Rails expects the join table to be named languanges\_users. Also, you  
> don’t want a model for this table, you simple create a migration:
> 
> class LanguagesUsers \< ActiveRecord::Migration  
> def self.up  
> create\_table :languages\_users, :id =\> false do |t|  
> t.column :language\_id, :integer, :null =\> false  
> t.column :user\_id, :integer, :null =\> false  
> end  
> end
> 
> def self.down  
> drop\_table :languages\_users  
> end  
> end

Thanks. It fixes my problem.

I am totally new to ROR, how can I know when I have to use  
languages\_users rather than users\_languages?

Is there any tutorial for naming convention? thanks.

---

<div class="post-metadata">

**Author:** ![surfman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@surfman](https://www.ruby-forum.com/u/surfman)\
**Post date:** [February 20, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208/4 "2007-02-20T18:12:24Z")

</div>

> I am totally new to ROR, how can I know when I have to use  
> languages\_users rather than users\_languages?

It’s based on which word comes first in the alphabet, hence  
languages\_users rather than users\_languages.

-Drew

---

<div class="post-metadata">

**Author:** ![surfman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@surfman](https://www.ruby-forum.com/u/surfman)\
**Post date:** [February 20, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/many-to-many-error-on-relation/86208/5 "2007-02-20T20:43:48Z")

</div>

Yes. There are tons of tutorials, books, and such. Also there is  
always the Rails and Ruby API.  
Agile web development with rails has an excellant chapter on  
relationship so does the RoR website’s How To section.

On Feb 20, 9:04 am, Surfman J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
