# Messed-up habtm relationship

**URL:** <https://www.ruby-forum.com/t/messed-up-habtm-relationship/75905>\
**Category:** Rails\
**Created:** [November 14, 2006, 6:19am UTC](https://www.ruby-forum.com/t/messed-up-habtm-relationship/75905 "2006-11-14T06:19:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [November 14, 2006, 6:19am UTC](https://www.ruby-forum.com/t/messed-up-habtm-relationship/75905/1 "2006-11-14T06:19:29Z")

</div>

Hi,

I have a many-to-many relationship between dishes and ingredients

class Dish \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :ingredients  
end

class Ingredient \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :dishes  
end

I have a join table dishes\_ingredients

now when i access my\_dish.ingredients, i get a list of what looks like  
Ingredient objects, but the but they don’t have the right attributes:

#\<Ingredient:0x29d3544 @readonly=true, @attributes={“name”=\>“Chicken”,  
“ingredient\_id”=\>“36”, “dish\_id”=\>“1”, “id”=\>“1”}\>

The join table’s columns ‘dish\_id’ and ‘ingredient\_id’ show up, and the  
id is the id of the row in the join table, not the one of the ingredient  
(Chicken).

I noticed this because the following was false where it should have been  
true:

my\_dish.ingredients.include?(chicken)

Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [November 14, 2006, 7:25am UTC](https://www.ruby-forum.com/t/messed-up-habtm-relationship/75905/2 "2006-11-14T07:25:43Z")

</div>

I think you may have confused what include? does.

The description (Ruby doc) is:

Returns true if any member of enum equals obj. Equality is tested using  
==.

When testing your Ingredient objects for equality, unless you have  
specifically overridden == in your model, what’s being compared to  
‘Chicken’  
is actually an object of type Ingredient.

Consider:

my\_dish.ingredients.find{|ingredient| ingredient.name == ‘Chicken’}

I’m pretty sure there’s a better way to do this, but find is always  
reliable.

Hope this helps.

Keynan P. wrote:

> class Ingredient \< ActiveRecord::Base
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
View this message in context:  
[http://www.nabble.com/-Rails--messed-up-habtm-relationship-tf2627404.html#a7332184](http://www.nabble.com/-Rails--messed-up-habtm-relationship-tf2627404.html#a7332184)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [November 14, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/messed-up-habtm-relationship/75905/3 "2006-11-14T15:19:11Z")

</div>

I found what the problem was: I had accidentally given the  
dishes\_ingredients join table an ‘ID’ column, and this was messing  
things up.

Thanks for all your help!

Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [November 14, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/messed-up-habtm-relationship/75905/4 "2006-11-14T14:12:50Z")

</div>

Thanks, Ross!

Actually, in my example the variable ‘chicken’ is thought to contain a  
‘Ingredient’ object. So I should have written:

my\_dish = Dish.find(:first)  
chicken = Ingredient.find\_by\_name(“Chicken”)  
my\_dish.ingredients.include?(chicken)

That should work, or? But it doesn’t!

Ingo

Steve R. wrote:

> I think you may have confused what include? does.
> 
> The description (Ruby doc) is:
> 
> Returns true if any member of enum equals obj. Equality is tested using  
> ==.
> 
> When testing your Ingredient objects for equality, unless you have  
> specifically overridden == in your model, what’s being compared to  
> ‘Chicken’  
> is actually an object of type Ingredient.
> 
> Consider:
> 
> my\_dish.ingredients.find{|ingredient| ingredient.name == ‘Chicken’}
> 
> I’m pretty sure there’s a better way to do this, but find is always  
> reliable.
> 
> Hope this helps.
> 
> Keynan P. wrote:
> 
> > class Ingredient \< ActiveRecord::Base
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> View this message in context:  
> [http://www.nabble.com/-Rails--messed-up-habtm-relationship-tf2627404.html#a7332184](http://www.nabble.com/-Rails--messed-up-habtm-relationship-tf2627404.html#a7332184)  
> Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
