Association problems?

Hiho,

yep, I'm a newbie :wink: 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 %>
    <li> <%= 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

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:

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

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

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

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

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

I'd call that a bug in AR.

Fred