Hi, Newbie here. I''m having to enter this from memory since my work is on a closed network, so forgive any syntax errors. I''m trying to recreate some basic Google Plus functionality with regards to users, posts, and circles - that is, a user can see a post only if he/she is in one or more of the same circles that the post belongs to: class User has_and_belongs_to :circles has_many :posts class Post has_and_belongs_to :circles belongs_to :user # the user that created the post class Circle has_and_belongs_to :posts has_and_belongs_to :users Given the above, how can I retrieve all posts viewable by a user? The SQL (I think) would be: select distinct p.id from posts p, circles_posts cp, circles_users cu where p.id = circles_posts.post_id and cp.circle_id = cu.circle_id and cu.user_id = current_user.id I also need to be able to retrieve posts related to a subset of circles to which the user belongs: select distinct p.id from posts p, circles_posts cp, circles_users cu where p.id = circles_posts.post_id and cp.circle_id = cu.circle_id and cu.circle_id in (4,5,6) and cu.user_id = current_user.id Can someone help? I can handle the basic RoR associations but this one is making my brain explode. :) Thanks _very_ much, Stan McFarland -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 August 2011 21:44, Stan McFarland <stan.mcfarland-zblGgqbDkCuC8CmZemzhMw@public.gmane.org> wrote:> Hi, > > Newbie here. I''m having to enter this from memory since my work is on > a closed network, so forgive any syntax errors. > > I''m trying to recreate some basic Google Plus functionality with > regards to users, posts, and circles - that is, a user can see a post > only if he/she is in one or more of the same circles that the post > belongs to: > > class User > has_and_belongs_to :circles > has_many :posts > class Post > has_and_belongs_to :circles > belongs_to :user # the user that created the post > class Circle > has_and_belongs_to :posts > has_and_belongs_to :usersFirstly each of those should be has_and_belongs_to_many, but I expect you knew that. Secondly, I don''t know whether you realise, but I don''t think the above will fully model the google+ concept. I think you need to model the asymmetric way that circles relate to users. I think you will need two relationships, firstly user has_many circles and circle belongs to user. This will map the fact that a user has a number of circles. Then you need to record who is *in* that circle via a has_and_belongs_to_many relationships, where you will need to use the :class_name specifier. Something like user has_and_belongs_to_many :member_of_circles, :class_name=>''Circle'' Then the circles owned by a user are @user.circles and the circles that the user is in are @user.member_of_circles You will also need the equivalent reverse relationship to get all the users that are members of a circle. I have not tested that, in fact I am not sure I have used :class_name with a has_and_belongs_to relationship, I tend to prefer explicit join tables. If you are a beginner at Rails I think you might be better to start with something simpler, this is a non-trivial problem for the beginner. Have you worked through some good tutorials? I recommend railstutorial.org which is free to use online. Make sure you are using Rails 3 and that the tutorial is for Rails 3. Also work through the Rails Guides. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Thanks for the feedback. You''re right about the asymmetry of the circles- I''ll work on that. I''d pick a simpler problem to solve but this is what my client is paying me for. I''ve actually built a few rails projects but they were really straightforward. Thanks again Sent from my iPhone On Aug 9, 2011, at 3:50 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 8 August 2011 21:44, Stan McFarland <stan.mcfarland-zblGgqbDkCuC8CmZemzhMw@public.gmane.org> wrote: >> Hi, >> >> Newbie here. I''m having to enter this from memory since my work is on >> a closed network, so forgive any syntax errors. >> >> I''m trying to recreate some basic Google Plus functionality with >> regards to users, posts, and circles - that is, a user can see a post >> only if he/she is in one or more of the same circles that the post >> belongs to: >> >> class User >> has_and_belongs_to :circles >> has_many :posts >> class Post >> has_and_belongs_to :circles >> belongs_to :user # the user that created the post >> class Circle >> has_and_belongs_to :posts >> has_and_belongs_to :users > > Firstly each of those should be has_and_belongs_to_many, but I expect > you knew that. > Secondly, I don''t know whether you realise, but I don''t think the > above will fully model the google+ concept. I think you need to model > the asymmetric way that circles relate to users. I think you will > need two relationships, firstly user has_many circles and circle > belongs to user. This will map the fact that a user has a number of > circles. Then you need to record who is *in* that circle via a > has_and_belongs_to_many relationships, where you will need to use the > :class_name specifier. Something like > user has_and_belongs_to_many :member_of_circles, :class_name=>''Circle'' > Then the circles owned by a user are > @user.circles > and the circles that the user is in are > @user.member_of_circles > You will also need the equivalent reverse relationship to get all the > users that are members of a circle. > > I have not tested that, in fact I am not sure I have used :class_name > with a has_and_belongs_to relationship, I tend to prefer explicit join > tables. > > If you are a beginner at Rails I think you might be better to start > with something simpler, this is a non-trivial problem for the > beginner. Have you worked through some good tutorials? I recommend > railstutorial.org which is free to use online. Make sure you are > using Rails 3 and that the tutorial is for Rails 3. > > Also work through the Rails Guides. > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.