I’m playing out with writing an online store.
So far it has accounts, addressbooks, and orders, and those 3 are tied
together with an customer ID. I also have orderDetails, which are
associated to the order via an order number. See models below…
When I do the following I can access the orderDetails information just
fine:
@order = Order.find(params[:id]) # i pass it a customer id
and i could access the orderDetails by doing this:
<%for detail in @order.orderDetails %>
item #: <%=detail.itemId%><br>etc etc
<%end%>
However, not all customers have orders, so it sometimes would return
RecordNotFound error.
So, i changed it to:
@order = Order.find(:all, :conditions => ["customerId = ?",
params[‘customerId’]])
which still works. But, now i cannot access the orderDetails the same
way, it says:
undefined method `orderDetails’ for []:Array
Have no clue what I’m doing wrong.
Does just plain old find return something different than find :all?
Here are my models:
class Account < ActiveRecord::Base
set_primary_key ‘customerId’
has_many :addressbooks, :foreign_key => ‘customerId’
has_many :orders, :foreign_key => ‘customerId’
end
class Addressbook < ActiveRecord::Base
belongs_to :account
end
class Order < ActiveRecord::Base
set_primary_key “customerId”
belongs_to :account
has_many :orderDetails, :foreign_key => ‘orderNumber’, :finder_sql =>
'SELECT * ’ +
'FROM orderDetails od ’ +
‘WHERE od.orderNumber = #{orderNumber}’
end
class OrderDetail < ActiveRecord::Base
set_table_name “orderDetails”
belongs_to :order
end
Thanks!