Has_one

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!

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.

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!

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 :slight_smile:

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:

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! Ruby for Rails

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
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.