# Association problems?

**URL:** <https://www.ruby-forum.com/t/association-problems/130265>\
**Category:** Rails\
**Created:** [February 26, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/association-problems/130265 "2008-02-26T21:54:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olav\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Olav\_MSSS\_ller](https://www.ruby-forum.com/u/Olav_MSSS_ller)\
**Post date:** [February 26, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/association-problems/130265/1 "2008-02-26T21:54:23Z")

</div>

Hiho,

yep, I’m a newbie 😉 I can’t get this to work.

I have the classes “Wishlist” and “User”.

class Wishlist \< ActiveRecord::Base  
belongs\_to :owner, :class\_name =\> “User”, :foreign\_key =\> “user\_id”  
has\_and\_belongs\_to\_many :visitors, :class\_name =\> “User”  
end

class User \< ActiveRecord::Base  
has\_many :wishlists  
has\_and\_belongs\_to\_many :observations, :class\_name =\> “Wishlist”  
end

and this (partial) schema.rb:

ActiveRecord::Schema.define(:version =\> 4) do  
create\_table “users”, :force =\> true do |t|  
t.string “name”  
[…]  
end

create\_table “users\_wishlists”, :id =\> false, :force =\> true do |t|  
t.integer “user\_id”  
t.integer “wishlist\_id”  
end

create\_table “wishlists”, :force =\> true do |t|  
t.integer “user\_id”  
[…]  
end

end

Now, when I do something like this in my view:

\<% for ll in @current\_user.observations %\>

- \<%= ll.owner.name %\> \<%= ll.titel %\> von \<%= ll.owner.name %\>  
\<% end %\>

the “owner” of ll is not set correctly but to @current\_user

WHAT am I doing wrong? I am quite lost.

Thanks for your help,  
Olav

---

<div class="post-metadata">

**Author:** ![Olav\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Olav\_MSSS\_ller](https://www.ruby-forum.com/u/Olav_MSSS_ller)\
**Post date:** [February 26, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/association-problems/130265/2 "2008-02-26T22:52:47Z")

</div>

I do not have your answer. I have only some general remarks :  
Have you tested your associations ? It is always good to test them,  
specially when you are a newbie.  
To test them you have 2 options :  
Create unit tests  
Use the console (script/console) and type (or copy/paste) your code like  
:  
wl = Wishlist.find(1)  
wl.owner  
wl.owner.name  
u1 = User.find(1)  
u1.wishlists  
u1.observations

See what happens.

H  
Olav MÃ¼ller wrote:

> Hiho,
> 
> yep, I’m a newbie 😉 I can’t get this to work.
> 
> I have the classes “Wishlist” and “User”.
> 
> class Wishlist \< ActiveRecord::Base  
> belongs\_to :owner, :class\_name =\> “User”, :foreign\_key =\> “user\_id”  
> has\_and\_belongs\_to\_many :visitors, :class\_name =\> “User”  
> end
> 
> class User \< ActiveRecord::Base  
> has\_many :wishlists  
> has\_and\_belongs\_to\_many :observations, :class\_name =\> “Wishlist”  
> end
> 
> and this (partial) schema.rb:
> 
> ActiveRecord::Schema.define(:version =\> 4) do  
> create\_table “users”, :force =\> true do |t|  
> t.string “name”  
> […]  
> end
> 
> create\_table “users\_wishlists”, :id =\> false, :force =\> true do |t|  
> t.integer “user\_id”  
> t.integer “wishlist\_id”  
> end
> 
> create\_table “wishlists”, :force =\> true do |t|  
> t.integer “user\_id”  
> […]  
> end
> 
> end
> 
> Now, when I do something like this in my view:
> 
> \<% for ll in @current\_user.observations %\>
> 
> - \<%= ll.owner.name %\> \<%= ll.titel %\> von \<%= ll.owner.name %\>  
> \<% end %\>
> 
> the “owner” of ll is not set correctly but to @current\_user
> 
> WHAT am I doing wrong? I am quite lost.
> 
> Thanks for your help,  
> Olav

---

<div class="post-metadata">

**Author:** ![Olav\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Olav\_MSSS\_ller](https://www.ruby-forum.com/u/Olav_MSSS_ller)\
**Post date:** [February 26, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/association-problems/130265/3 "2008-02-26T23:46:16Z")

</div>

Hiho,

> Have you tested your associations ? It is always good to test them,

thanks for the quick reply. Yes, I have tested my associations and  
they give me the same (false) results:

sqlite\> select \* from users;  
1|user1|2008-02-26 23:20:42|2008-02-26 23:20:42  
2|user2|2008-02-26 23:20:49|2008-02-26 23:20:49

sqlite\> select \* from wishlists;  
1|1|wishlist1|2008-02-26 23:25:06|2008-02-26 23:25:06  
2|1|wishlist2|2008-02-26 23:25:26|2008-02-26 23:25:26

sqlite\> select \* from users\_wishlists;  
2|1  
2|2

This should give me two wishlists (both owned by user1) and each  
observed by user2. I think the database is correct here. Even though  
console says:

> > u2 = User.find(2)  
> > u2 = User.find(2)  
> > =\> #\<User id: 2, name: “user2”, created\_at: “2008-02-26 23:20:49”,  
> > updated\_at: “2008-02-26 23:20:49”\>
> 
> > u2.observations  
> > u2.observations  
> > =\> [#\<Wishlist id: 1, user\_id: 2, titel: “wishlist1”, created\_at:  
> > “2008-02-26 23:25:06”, updated\_at: “2008-02-26 23:25:06”\>, #\<Wishlist  
> > id: 2, user\_id: 2, titel: “wishlist2”, created\_at: “2008-02-26  
> > 23:25:26”, updated\_at: “2008-02-26 23:25:26”\>]
> 
> > w1 = Wishlist.find( 1 )  
> > w1 = Wishlist.find( 1 )  
> > =\> #\<Wishlist id: 1, user\_id: 1, titel: “wishlist1”, story: “”,  
> > created\_at: “2008-02-26 23:25:06”, updated\_at: “2008-02-26 23:25:06”\>

In u2.observations the lists are returned with user\_id set to 2, which  
is NOT the owner as can be seen in w1, where user\_id is (correctly)  
set to 1.

Is there a problem when I try to associate two classes to one another  
several times? From Wishlist both :owner and :visitors point to Users.  
Or am I doing something different wrong in my model?

Thanks,  
Olav

---

<div class="post-metadata">

**Author:** ![Olav\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Olav\_MSSS\_ller](https://www.ruby-forum.com/u/Olav_MSSS_ller)\
**Post date:** [February 27, 2008, 8:23am UTC](https://www.ruby-forum.com/t/association-problems/130265/4 "2008-02-27T08:23:35Z")

</div>

On 26 Feb 2008, at 22:45, Olav Müller wrote:

> In u2.observations the lists are returned with user\_id set to 2, which  
> is NOT the owner as can be seen in w1, where user\_id is (correctly)  
> set to 1.
> 
> Is there a problem when I try to associate two classes to one another  
> several times? From Wishlist both :owner and :visitors point to Users.  
> Or am I doing something different wrong in my model?

What’s the query (check your logs) when you get  
current\_user.observations? I’m betting that it boils down to select \*  
from user\_wishlists inner join wishlists on… and that the user\_id  
column on the join table is clobbering the user\_id on the wishlist  
table, which is why you’re always getting the user\_id of the current  
user. The easiest way is probably to rename the user\_id column on  
wishlist (eg to owner\_id

Fred

---

<div class="post-metadata">

**Author:** ![Olav\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Olav\_MSSS\_ller](https://www.ruby-forum.com/u/Olav_MSSS_ller)\
**Post date:** [February 27, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/association-problems/130265/5 "2008-02-27T13:06:43Z")

</div>

On 27 Feb 2008, at 11:53, Olav Müller wrote:

> much.
> 
> > The easiest way is probably to rename the user\_id column on  
> > wishlist (eg to owner\_id
> 
> Yes. Works great!
> 
> Only one question remains: Is this expected behaviour (how so?), a bug  
> in my model description (probably) or some glitch in ActiveRecord  
> which should be reported?

I’d call that a bug in AR.

Fred

---

<div class="post-metadata">

**Author:** ![Olav\_MSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Olav\_MSSS\_ller](https://www.ruby-forum.com/u/Olav_MSSS_ller)\
**Post date:** [February 27, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/association-problems/130265/6 "2008-02-27T12:54:17Z")

</div>

Hiho,

> What’s the query (check your logs) when you get  
> current\_user.observations? I’m betting that it boils down to select \*  
> from user\_wishlists inner join wishlists on… and that the user\_id  
> column on the join table is clobbering the user\_id on the wishlist  
> table

Yes, that is exactly what seems to have happened. Thank you very  
much.

> The easiest way is probably to rename the user\_id column on  
> wishlist (eg to owner\_id

Yes. Works great!

Only one question remains: Is this expected behaviour (how so?), a bug  
in my model description (probably) or some glitch in ActiveRecord  
which should be reported?

Thanks again,  
Olav
