Hi guys I am not sure if it is actually possible what I am trying to do, but Im sure you can help me with it. Let me start with my models: class User < ActiveRecord::Base has_many :wall_posts, :class_name => "Post", :foreign_key => "wall_id", :order => ''created_at DESC'' has_many :invites, :class_name => "Invite", :foreign_key => "contact_id", :order => ''created_at DESC'' end Now what I want is to combine this to a sort of stream (facebook style). This works fine if I do it with an array as in: def stream stream = wall_posts + invites end but in my view I am using a little bit of javascript and ajax to make an ''endless'' page. So of course I am using limit and offset. This works fine on SQL queries but not for the arrays. So obviously it is then not possible to write user.stream.limit(6).offset(6) because its already an array.. i tried different joins on posts but i cant really get it to work properly. I have to mix them into a stream so that they arrange properly chronologically. Thank you for your help, I appreciate it. Kind regards Stefano -- 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 Jul 30, 4:36 pm, Stefano <stefano....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys > > I am not sure if it is actually possible what I am trying to do, but > Im sure you can help me with it. > Let me start with my models: > > class User < ActiveRecord::Base > has_many :wall_posts, :class_name => "Post", :foreign_key => > "wall_id", :order => ''created_at DESC'' > has_many :invites, :class_name => "Invite", :foreign_key => > "contact_id", :order => ''created_at DESC'' > end > > Now what I want is to combine this to a sort of stream (facebook > style). This works fine if I do it with an array as in: > > def stream > stream = wall_posts + invites > end > > but in my view I am using a little bit of javascript and ajax to make > an ''endless'' page. So of course I am using limit and offset. This > works fine on SQL queries but not for the arrays. So obviously it is > then not possible to write user.stream.limit(6).offset(6) because its > already an array.. > > i tried different joins on posts but i cant really get it to work > properly. I have to mix them into a stream so that they arrange > properly chronologically.Well the slice method on array does sort of the same thing as limit/ offset, eg [1,2,3,4,5,6,7,8,9,10].slice(3,4) #=> [4, 5, 6, 7] However if you were to do stream.slice(...) then that would load all the wall_posts and invites before slicing. If you want to load only those that are actually going to be displayed then you''ll have to come up with something different, eg passing in both the offset for wall_posts and the one for invites. Fred> > Thank you for your help, I appreciate it. > > Kind regards > Stefano-- 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.
Yeah I thought about that but it wouldnt work beacuse the last 10 things might only be posts and no invites. yet I would still try to fetch invites. Also I think it would get very confusing with offset if not impossible. I tried to ''extend'' the post item but it doesnt quite feel right.. I could make another model for stream but that just feels like im putting layer on top of layer trying to get stuff sorted... I would then need a polymorphic association for a stream item so in a stream there can be other objects but then on the view layer I add another layer... it just feels like there must be a nicer better way of doing it. Fetching all the records and then sorting doesnt seem like an option a year on from now when some people might have 2000 posts and 500 invites.. can get pretty heavy on the DB :P But thanks for the input Frederick. On Jul 30, 5:55 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 30, 4:36 pm, Stefano <stefano....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi guys > > > I am not sure if it is actually possible what I am trying to do, but > > Im sure you can help me with it. > > Let me start with my models: > > > class User < ActiveRecord::Base > > has_many :wall_posts, :class_name => "Post", :foreign_key => > > "wall_id", :order => ''created_at DESC'' > > has_many :invites, :class_name => "Invite", :foreign_key => > > "contact_id", :order => ''created_at DESC'' > > end > > > Now what I want is to combine this to a sort of stream (facebook > > style). This works fine if I do it with an array as in: > > > def stream > > stream = wall_posts + invites > > end > > > but in my view I am using a little bit of javascript and ajax to make > > an ''endless'' page. So of course I am using limit and offset. This > > works fine on SQL queries but not for the arrays. So obviously it is > > then not possible to write user.stream.limit(6).offset(6) because its > > already an array.. > > > i tried different joins on posts but i cant really get it to work > > properly. I have to mix them into a stream so that they arrange > > properly chronologically. > > Well the slice method on array does sort of the same thing as limit/ > offset, eg > > [1,2,3,4,5,6,7,8,9,10].slice(3,4) #=> [4, 5, 6, 7] > > However if you were to do stream.slice(...) then that would load all > the wall_posts and invites before slicing. If you want to load only > those that are actually going to be displayed then you''ll have to come > up with something different, eg passing in both the offset for > wall_posts and the one for invites. > > Fred > > > > > > > Thank you for your help, I appreciate it. > > > Kind regards > > Stefano-- 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.
On 30 July 2011 16:36, Stefano <stefano.hug-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi guys > > I am not sure if it is actually possible what I am trying to do, but > Im sure you can help me with it. > Let me start with my models: > > class User < ActiveRecord::Base > has_many :wall_posts, :class_name => "Post", :foreign_key => > "wall_id", :order => ''created_at DESC'' > has_many :invites, :class_name => "Invite", :foreign_key => > "contact_id", :order => ''created_at DESC'' > end > > Now what I want is to combine this to a sort of stream (facebook > style). This works fine if I do it with an array as in: > > def stream > stream = wall_posts + invites > endYou could use STI and combine the two classes into one table. Then the stream would be trivial I think. 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.
I guess so. It just seems to grow endles and the queries for the DB seem to get more and more complicated. Well I guess thats the way to go then. Thanks Colin. I wonder when you hear from Amirite again ;) On Jul 30, 7:00 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 30 July 2011 16:36, Stefano <stefano....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > Hi guys > > > I am not sure if it is actually possible what I am trying to do, but > > Im sure you can help me with it. > > Let me start with my models: > > > class User < ActiveRecord::Base > > has_many :wall_posts, :class_name => "Post", :foreign_key => > > "wall_id", :order => ''created_at DESC'' > > has_many :invites, :class_name => "Invite", :foreign_key => > > "contact_id", :order => ''created_at DESC'' > > end > > > Now what I want is to combine this to a sort of stream (facebook > > style). This works fine if I do it with an array as in: > > > def stream > > stream = wall_posts + invites > > end > > You could use STI and combine the two classes into one table. Then > the stream would be trivial I think. > > 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.
On 30 July 2011 18:10, Stefano <stefano.hug-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I guess so. It just seems to grow endles and the queries for the DB > seem to get more and more complicated. > > Well I guess thats the way to go then. Thanks Colin. > > I wonder when you hear from Amirite again ;)Sorry, you have lost me. 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.