rails.nerd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Feb-22 09:43 UTC
Q: find_by_x_id and named scopes
Hi I have a model Topic that has many Posts, and I''m trying to use a named scope on my find I can do something like this: @posts = @topic.posts.named_scope_func But I cannot do this: @posts = Post.find_by_topic_id(1).named_scope_func The reason is that the "posts" and "find_by_topic_id" seem to return different classes (Post and Array) Am I doing something wrong? thanks --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey, Named scopes generate a Class method. Looks like you''re trying to call one on an instance of Post (or an array with one Post in it) You could try specifying the topic first and then calling posts.named_scope i.e. @topic = Topic.find(1).posts.named_scope Don''t know any other way around this using named scopes I''m afraid On Feb 22, 9:43 am, "rails.n...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <rails.n...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I have a model Topic that has many Posts, and I''m trying to use a > named scope on my find > > I can do something like this: > > @posts = @topic.posts.named_scope_func > > But I cannot do this: > > @posts = Post.find_by_topic_id(1).named_scope_func > > The reason is that the "posts" and "find_by_topic_id" seem to return > different classes (Post and Array) > > Am I doing something wrong? > > thanks--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Hey, Named scopes generate a Class method. Looks like you''re trying to call one on an instance of Post (or an array with one Post in it). Don''t know any other way around this other than calling the class method on Topic.posts.named_scope Maybe @posts = Post.named_scope.all :conditions => {:topic_id => 1} On Feb 22, 9:43 am, "rails.n...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <rails.n...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > I have a model Topic that has many Posts, and I''m trying to use a > named scope on my find > > I can do something like this: > > @posts = @topic.posts.named_scope_func > > But I cannot do this: > > @posts = Post.find_by_topic_id(1).named_scope_func > > The reason is that the "posts" and "find_by_topic_id" seem to return > different classes (Post and Array) > > Am I doing something wrong? > > thanks--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---