# Error on relations

**URL:** <https://www.ruby-forum.com/t/error-on-relations/177005>\
**Category:** Rails\
**Created:** [October 9, 2009, 4:22pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005 "2009-10-09T16:22:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 4:22pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/1 "2009-10-09T16:22:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/2 "2009-10-09T16:29:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/3 "2009-10-09T16:41:17Z")

</div>

Ilan B. 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’”

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/4 "2009-10-09T17:04:57Z")

</div>

Ilan B. wrote:

> PÃ¥l BergstrÃ¶m wrote:

> that’s because you defined baskets as having MANY products… therefore  
> it’s:
> 
> item.products.each do |product|
> 
> # more code to deal with each product
> 
> end
> 
> hth
> 
> ilan

thanks 🙂

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/5 "2009-10-09T16:55:28Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> I get @mybasket. But I can’t access product info. It says “undefined  
> method `product’”

that’s because you defined baskets as having MANY products… therefore  
it’s:

item.products.each do |product|

# more code to deal with each product

end

hth

ilan

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 5:09pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/6 "2009-10-09T17:09:56Z")

</div>

Ilan B. 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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 9, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/error-on-relations/177005/7 "2009-10-09T17:31:20Z")

</div>

Marnen Laibow-Koser wrote:

> “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?

I meant exactly what you stated… 🙂
