Another newbie question. My dummy app has the following models:

class User < ActiveRecord::Base
has_one :list
end

class List < ActiveRecord::Base
belongs_to :user
has_many :celebs
end

class Celeb < ActiveRecord::Base
belongs_to :list
end

I want to load a user's list, and include the celebs that belong to it. I
tried the following:

@user = current_user

... @list = user.list.joins(:celebs)
... @list = user.list(:include => :celebs)
... @list = user.list.includes(:celebs)

The result was some errors/unexpect output. The expected query is:

SELECT * FROM lists INNER JOIN lists.id ON celebs.list_id WHERE
lists.user_id = @user_id

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QtXHPBoCAOwJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Peter Vandenabeele at Jan 11, 2012 at 10:51 am

    On Tue, Jan 10, 2012 at 11:23 PM, Mohamad El-Husseini wrote:

    Another newbie question. My dummy app has the following models:

    class User < ActiveRecord::Base
    has_one :list
    Try to add here:

    has_one :list_with_celebs, :class_name => "List", :include => :celebs


    end
    class List < ActiveRecord::Base
    belongs_to :user
    has_many :celebs
    end

    class Celeb < ActiveRecord::Base
    belongs_to :list
    end

    I want to load a user's list, and include the celebs that belong to it. I
    tried the following:

    @user = current_user

    ... @list = user.list.joins(:celebs)
    ... @list = user.list(:include => :celebs)
    ... @list = user.list.includes(:celebs)
    @user.list_with_celebs

    HTH,

    Peter

    --
    Peter Vandenabeele
    http://twitter.com/peter_v
    http://rails.vandenabeele.com

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Thiagocifani at Jan 11, 2012 at 12:31 pm
    You can try This way

    @user.includes(celebs)

    Sent from my iPad
    On 11/01/2012, at 08:50, Peter Vandenabeele wrote:

    On Tue, Jan 10, 2012 at 11:23 PM, Mohamad El-Husseini wrote:
    Another newbie question. My dummy app has the following models:

    class User < ActiveRecord::Base
    has_one :list

    Try to add here:

    has_one :list_with_celebs, :class_name => "List", :include => :celebs


    end

    class List < ActiveRecord::Base
    belongs_to :user
    has_many :celebs
    end

    class Celeb < ActiveRecord::Base
    belongs_to :list
    end

    I want to load a user's list, and include the celebs that belong to it. I tried the following:

    @user = current_user

    ... @list = user.list.joins(:celebs)
    ... @list = user.list(:include => :celebs)
    ... @list = user.list.includes(:celebs)

    @user.list_with_celebs

    HTH,

    Peter

    --
    Peter Vandenabeele
    http://twitter.com/peter_v
    http://rails.vandenabeele.com
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Colin Law at Jan 11, 2012 at 3:10 pm

    On 10 January 2012 22:23, Mohamad El-Husseini wrote:
    Another newbie question. My dummy app has the following models:

    class User < ActiveRecord::Base
    has_one :list
    end

    class List < ActiveRecord::Base
    belongs_to :user
    has_many :celebs
    end

    class Celeb < ActiveRecord::Base
    belongs_to :list
    end

    I want to load a user's list, and include the celebs that belong to it. I
    tried the following:

    @user = current_user

    ... @list = user.list.joins(:celebs)
    ... @list = user.list(:include => :celebs)
    ... @list = user.list.includes(:celebs)
    You don't normally need to use joins or includes for such things. you
    can just say
    @list = @user.list
    then
    celebs = @list.celebs
    or just
    celebs = @user.list.celebs.
    You may have to test for the case where @user.list is nil if it is
    possible to have a user that has no list

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 11, '12 at 10:35a
activeJan 11, '12 at 3:10p
posts4
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase