Error on relations

Pål Bergström wrote:

I have a controller and model for both "products" and "baskets". I've
set up the belongs_to and has_many. When I'm in a third controller
"shop" I get an error when I try to get the "products" data via
"baskets". So asking for item.product.price gives an error.

Can I have this kind of relation when I'm in a third controller, that
doesn't have a model itself?

Controllers have nothing to do with models, although best practices
suggests having a controller per model.

A controller has visibility into all your models if it needs to..

Please show us your relations that you wrote in your models and the
controller code used to access them and then we may be better prepared
to assist you

ilan

Ilan Berci wrote:

Pål Bergström wrote:

Controllers have nothing to do with models, although best practices
suggests having a controller per model.

A controller has visibility into all your models if it needs to..

Please show us your relations that you wrote in your models and the
controller code used to access them and then we may be better prepared
to assist you

ilan

class Basket < ActiveRecord::Base
  has_many :products
end

class Product < ActiveRecord::Base
  has_many :product_photos
  belongs_to :basket

  def self.created_at
    created_at.strftime("%Y-%m-%d")
  end
end

class ShopController < ApplicationController
  def basket
    @mybasket =
Basket.find_all_by_session_id(session[:basket_has_items])
  end
end

<% for item in @mybasket %>

<%= item.product.name %>

<% end %>

I get @mybasket. But I can't access product info. It says "undefined
method `product'"

Ilan Berci wrote:

Pål Bergström wrote:

I have a controller and model for both "products" and "baskets". I've
set up the belongs_to and has_many. When I'm in a third controller
"shop" I get an error when I try to get the "products" data via
"baskets". So asking for item.product.price gives an error.

Can I have this kind of relation when I'm in a third controller, that
doesn't have a model itself?

Controllers have nothing to do with models, although best practices
suggests having a controller per model.

"Best practices" suggest no such thing that I'm aware of. In a typical
app, most models will have associated controllers. Some controllers
will not have associated models, and a few models may not have
associated controllers. There's no reason to slavishly pair models and
controllers if the nature of the app doesn't call for it.

Or am I misunderstanding what you meant?

Best,