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 %>

  • <%= 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

  • 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 :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 %>

  • <%= 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:

    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

    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

    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

    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