# Newbie Confusion re underscores / camel case

**URL:** <https://www.ruby-forum.com/t/newbie-confusion-re-underscores-camel-case/173354>\
**Category:** Rails\
**Created:** [August 7, 2009, 8:41am UTC](https://www.ruby-forum.com/t/newbie-confusion-re-underscores-camel-case/173354 "2009-08-07T08:41:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [August 7, 2009, 8:41am UTC](https://www.ruby-forum.com/t/newbie-confusion-re-underscores-camel-case/173354/1 "2009-08-07T08:41:10Z")

</div>

Hi,

I’m a Java and ColdFusion developer trying to find my feet with JRuby  
and Rails.

I’ve created a very simple “Auction” app, with a database schema like  
this:

create\_table “auction\_items”, :force =\> true do |t|  
t.string “description”  
t.string “title”  
t.datetime “start\_date”  
t.datetime “end\_date”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

create\_table “bids”, :force =\> true do |t|  
t.integer “auction\_item\_id”  
t.decimal “amount”, :precision =\> 15, :scale =\> 2  
t.datetime “bid\_time”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

There are two classes in my model:

auction\_item.rb:

class AuctionItem \< ActiveRecord::Base  
has\_many :bids  
end

bid.rb:

class Bid \< ActiveRecord::Base  
belongs\_to :auction\_item  
end

Initially, I had bid.rb like this:

class Bid \< ActiveRecord::Base  
belongs\_to :AuctionItem  
end

(ie. using the camel-cased class name, as per the definintion in the  
class rather than the underscored one), but it didn’t work until I  
changed it to use the undscored one.

What is the rule here? Does the name in the “belongs\_to” refer to the  
table name or something?

Similarly, I’m a bit confused why I have to go to

[http://localhost:8080/auction\_items](http://localhost:8080/auction_items) (not  
[http://localhost:8080/AuctionItems](http://localhost:8080/AuctionItems)).

Can anyone explain the general rules to me, or point me to somewhere  
that does please?

Many thanks,  
Andrew.

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [August 7, 2009, 9:31am UTC](https://www.ruby-forum.com/t/newbie-confusion-re-underscores-camel-case/173354/2 "2009-08-07T09:31:26Z")

</div>

Hi

The first parameter to belongs\_to is the association id So here  
auction\_item\_id being the foreign key according to convension by rails  
we can say

belongs\_to :auction\_item

It will be taken as auction\_item\_id For example you can try

t.belongs\_to :auction\_item  
instead of  
t.integer “auction\_item\_id” in the above migration  
Defintion of belongs\_to you can see at

rails/activerecord/lib/active\_record/associations.rb, line 1001

Sijo

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [August 7, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/newbie-confusion-re-underscores-camel-case/173354/3 "2009-08-07T16:01:35Z")

</div>

Andrew M. wrote:  
[…]

> Can anyone explain the general rules to me, or point me to somewhere  
> that does please?

Standard Ruby convention (should be explained, at least in part, in the  
Pickaxe Book). Class names (like all constants) are uppercase.  
Everything else is lowercase. Unless you are using a literal class name  
(e.g. AuctionItem.find), you probably want lowercase.

The situation with has\_many is a little odd, since the symbol isn’t  
directly _anything_: for example, in has\_many :auction\_items, notice  
that the name is plural. This is Rails trying to do friendly  
English-like syntax.

> Many thanks,  
> Andrew.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![akm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@akm](https://www.ruby-forum.com/u/akm)\
**Post date:** [August 7, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/newbie-confusion-re-underscores-camel-case/173354/4 "2009-08-07T21:09:56Z")

</div>

Andrew M. wrote:

> Can anyone explain the general rules to me, or point me to somewhere  
> that does please?
> 
> Many thanks,  
> Andrew.

take a look at:  
ActiveSupport::CoreExtensions::String::Inflections

best way is to load it up in irb and play with it’s methods or look it  
up on the rails api… it will end all your confusion guaranteed.
