# Schema.rb naming conventions

**URL:** <https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114>\
**Category:** Rails\
**Created:** [December 14, 2007, 4:37am UTC](https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114 "2007-12-14T04:37:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 14, 2007, 4:37am UTC](https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114/1 "2007-12-14T04:37:02Z")

</div>

going by \<[http://wiki.rubyonrails.org/rails/pages/](http://wiki.rubyonrails.org/rails/pages/)  
ThroughAssociations\>, I think that I have my table names in the schema  
squared away, but I’m not convinced. For a many-to-many with  
ThroughAssociations between tables foo and bar, is the join table  
created named foo\_bars by convention? If the table name is foo\_bars,  
then the corresponding model in app/models would be FooBar, is that  
right?

If FooBar is created by me, then, going by the comments in schema.rb,  
a migration command was the correct way to create the schema.

I’m just not convinced that I’m following convention on this.

C:\code\strawr\db\>  
C:\code\strawr\db\>  
C:\code\strawr\db\>type schema.rb

# This file is autogenerated. Instead of editing this file, please use

the

# migrations feature of ActiveRecord to incrementally modify your

database, and

# then regenerate this schema definition.

ActiveRecord::Schema.define(:version =\> 3) do

create\_table “categories”, :force =\> true do |t|  
t.column “category”, :string, :default =\> “NULL”  
end

create\_table “category\_feeds”, :force =\> true do |t|  
t.column “category\_id”, :integer, :default =\> 0  
t.column “feed\_id”, :integer, :default =\> 0  
end

create\_table “feeds”, :force =\> true do |t|  
t.column “feed”, :string, :default =\> “NULL”  
end

end

C:\code\strawr\db\>

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 14, 2007, 4:43am UTC](https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114/2 "2007-12-14T04:43:30Z")

</div>

The table would be called bars\_foos. All join tables are the pluralized  
versions of what they’re joining in alphabetical order. Say I have  
orders  
and customers, the join table will be customers\_orders.

You don’t need to post long console feeds (unless we ask for it).  
Usually we  
can figure out what you want. The longer a post is, the less I feel like  
reading it. But that’s just me.

On Dec 14, 2007 2:06 PM, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a migration command was the correct way to create the schema.  
> database, and  
> t.column “feed\_id”, :integer, :default =\> 0
> 
> thanks,
> 
> Thufir
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 14, 2007, 9:07am UTC](https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114/3 "2007-12-14T09:07:51Z")

</div>

On Fri, 14 Dec 2007 14:11:10 +1030, Ryan B. wrote:

> The table would be called bars\_foos. All join tables are the pluralized  
> versions of what they’re joining in alphabetical order. Say I have  
> orders and customers, the join table will be customers\_orders.

Going by [http://grover.open2space.com/node/138](http://grover.open2space.com/node/138), the corresponding  
model  
would CustomerOrder, is that right?

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 14, 2007, 9:31am UTC](https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114/4 "2007-12-14T09:31:16Z")

</div>

On Fri, 14 Dec 2007 14:11:10 +1030, Ryan B. wrote:

> The table would be called bars\_foos. All join tables are the pluralized  
> versions of what they’re joining in alphabetical order. Say I have  
> orders and customers, the join table will be customers\_orders.

And I’ve come across this a few different ways:

Many-to-many

There are two ways to build a many-to-many relationship.

The first way uses a has\_many association with the :through option and a  
join model, so there are two stages of associations.

class Assignment \< ActiveRecord::Base  
belongs\_to :programmer # foreign key - programmer\_id  
belongs\_to :project # foreign key - project\_id  
end  
class Programmer \< ActiveRecord::Base  
has\_many :assignments  
has\_many :projects, :through =\> :assignments  
end  
class Project \< ActiveRecord::Base  
has\_many :assignments  
has\_many :programmers, :through =\> :assignments  
end

\<[Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Associations/)  
ClassMethods.html\>

In that example, the name for the join model doesn’t contain the models  
which it joins. I also saw a similar naming scheme in the rails recipe  
book, I believe. Aaaargh.

-Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 16, 2007, 9:12am UTC](https://www.ruby-forum.com/t/schema-rb-naming-conventions/123114/5 "2007-12-16T09:12:59Z")

</div>

On Dec 14, 1:51 am, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The first way uses a has\_many association with the :through option and a  
> join model, so there are two stages of associations.

If you want to use has\_many/through, then you should try to come up  
with a meaningful name for the relationship. If you are connecting  
foos to bars, I would only call the relationship table bars\_foos if I  
were going to use has\_and\_belongs\_to\_many.

Even though it’s now out of fashion, I see no reason not to use habtm  
for relationships with no data. If some data appears later, just  
rename the table and add some columns.
