# Has\_one

**URL:** <https://www.ruby-forum.com/t/has-one/81595>\
**Category:** Rails\
**Created:** [March 14, 2006, 4:50am UTC](https://www.ruby-forum.com/t/has-one/81595 "2006-03-14T04:50:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 14, 2006, 4:50am UTC](https://www.ruby-forum.com/t/has-one/81595/1 "2006-03-14T04:50:47Z")

</div>

I’ve got an order model that stores order data.  
One piece of data is a credit card type, which is a digit 1,2 or 3.

I have a cardType model that has an id, shortName and LongName for the  
credit card merchant (visa, mastercard, amex).

I want to be able to say: order.cardType.shortName, but can’t seem to  
get has\_one working. It works with has\_many and a finder\_sql statement  
on it, but that returns an array, so I have to access it like this:

order.cardTypes[0].shortName

The has\_one below doesn’t really work, because the sql query ends up  
looks something like this:

select \* from cardTypes where cardTypes.id = ‘theprimarykeyfororder’ and  
cardTypes.id = ‘blah’

I don’t want the cardTypes.id = 'theprimarykey" part in there.

Any help is greatly appriciated. My Order model is below:

class Order \< ActiveRecord::Base  
set\_primary\_key “customerId”  
belongs\_to :account

has\_many :orderStatuses, :finder\_sql =\>  
'SELECT name ’ +  
'FROM orderStatuses ors ’ +  
‘WHERE ors.id = “#{status}” LIMIT 1’  
has\_many :cardTypes, :foreign\_key =\> ‘id’, :finder\_sql =\>  
'SELECT shortName, longName ’ +  
'FROM cardTypes ct ’ +  
‘WHERE ct.id = #{cardType}’  
#has\_one :cardType, :foreign\_key =\> ‘id’, :conditions =\> ‘id =  
“”#{cardType}""’  
has\_many :orderDetails, :foreign\_key =\> ‘orderNumber’, :finder\_sql =\>  
'SELECT \* ’ +  
'FROM orderDetails od ’ +  
‘WHERE od.orderNumber = #{orderNumber}’  
end

class CardType \< ActiveRecord::Base  
set\_table\_name ‘cardTypes’  
belongs\_to :order  
end

Thanks!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 14, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/has-one/81595/2 "2006-03-14T14:24:38Z")

</div>

On Mar 13, 2006, at 10:50 PM, David C. wrote:

> order.cardTypes[0].shortName  
> Any help is greatly appriciated. My Order model is below:  
> has\_many :cardTypes, :foreign\_key =\> ‘id’, :finder\_sql =\>
> 
> class CardType \< ActiveRecord::Base  
> set\_table\_name ‘cardTypes’  
> belongs\_to :order  
> end

The :foreign\_key attribute you are setting is what is screwing things  
up. Well, that and the fact that this should be a belongs\_to, not a  
has\_one. You shouldn’t need any finder\_sql for such a simple  
association. Your model should simply be:

belongs\_to :card\_type, :foreign\_key =\> ‘cardType’

Note that the symbolic form of the CardType model is all lower case  
and separated by underscores. Also, the belongs\_to with the  
foreign\_key declaration tell Rails that the foreign\_key is on the  
Order table and is called ‘cardType’.

-Derrick S.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 14, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/has-one/81595/3 "2006-03-14T15:48:49Z")

</div>

Thanks! That worked great.

It seems that one of my big problems is how I’m naming things.

Is there a good site/tutorial out there that speaks to what should be  
named what?

class thingThing  
database table: stuff\_stuff

etc etc?

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 14, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/has-one/81595/4 "2006-03-14T16:15:00Z")

</div>

Hi –

On Tue, 14 Mar 2006, David C. wrote:

> Thanks! That worked great.
> 
> It seems that one of my big problems is how I’m naming things.
> 
> Is there a good site/tutorial out there that speaks to what should be  
> named what?
> 
> class thingThing

You’ll get a syntax error if you try that 🙂

> database table: stuff\_stuff
> 
> etc etc?

You’ll see the basics if you look at the Ruby standard library and/or  
the Rails source code and/or the Rails API docs. A couple of points  
to get you started:

- Class names are constants, LikeThis (starting with an uppercase  
letter).
- camelCase is generally not used in Ruby (though it’s legal),  
except in constant names that look LikeThis. For local variables  
and method names, use\_underscores. Try to avoidThis.

You might find this useful too:

> **[Ruby | zenspider.com | by ryan davis](https://www.zenspider.com/ruby/)**
>
> Ruby facts, opinions, source code, links, and stuff.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [March 14, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/has-one/81595/5 "2006-03-14T16:27:17Z")

</div>

On Mar 14, 2006, at 9:48 AM, David C. wrote:

> etc etc?
> 
> Thanks a bunch!

Yes, the power rails is only fully realized if you follow the  
conventions. I.e. Convention over Configuration. As to learning the  
conventions, of course the best is the book Agile Web D.  
with Rails. It will teach you the conventions as it teaches the  
framework. Financially, this is a great investment. The book is  
definitely worth the money - and that’s something I haven’t said  
about many programming books.

As for free instruction, there are several links on the rails site:  
[http://www.rubyonrails.com/docs](http://www.rubyonrails.com/docs)  
I don’t know if there is a one-stop-super-mega tutorial that will  
completely get you up to speed, but these docs, the mailing list, and  
IRC should get you going pretty quickly.

-Derrick S.
