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!