<% for shopping_cart_item in @shopping_cart.shopping_cart_items %>
Any suggestions?
@shopping_cart is nil. You’ll need to look at the my_shopping_cart
action of the Shoppingcart controller to figure out why that’s not
being set correctly.
Thanks Philip. I’m new here, can you advise on what I need to do prior
to get @shopping_cart set? I’m not sure of the exact steps involved in
accessing the data in ROR.
has_many :shopping_cart_items,
:class_name => “shopping_cart_item”,
:finder_sql => “select ti.* from shopping_cart_items ti,
shopping_carts t
where ti.owner_id = t.owner_id”
end
Your shopping_cart_item (singular) is probably a rails model and it
needs to start with a capital letter as it should be a ruby
constant.
When you define a model or any class in the normal way, you do
something like this
class ShoppingCartItem < ActiveRecord::Base
…
ShoppingCartItem is a ruby constant because it starts with capital (as
opposed to local variable or method name).
Also, you should probably use camel case instead of underscores for
constant names - that seems to be a convention but with rails it might
be a requirement (not completely sure as I’ve never done it that way).
You’d then refer to this class using class_name =>
‘ShoppingCartItem’ .
Your underlying table can stay as shopping_cart_items, of course.
You could probably dispense with class_name altogether if
“has_many :shopping_cart_items” refers to model ShoppingCartItem.
Rails will infer that for you.
has_many :shopping_cart_items,
:class_name => “ShoppingCartItems”,
:finder_sql => “select ti.* from shopping_cart_items ti,
shopping_carts t where ti.owner_id = t.owner_id”
end
and my shoppingcartitem.rb defines
class ShoppingCartItem
I feel like I’m close, but I can’t figure this out. Can you figure out
the missing link?
You can name your files like: shopping_cart_item.rb but define the
class inside as ‘ShoppingCartItem’. Similarly for shopping_cart.rb
and class ShoppingCart.
Glad it helped.
–
Daniel B.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.