Hi there ! Consider the following 2 initial models: =======================================project: -------- has_many :tasks task: ----- belongs_to :project ======================================= My question: Is there a way in Rails to define some sort of a "sub model", like this? (And if yes, how exactly?) project: -------- has_many :tasks has_many :unfinished_tasks, :class => ''task'', :conditions => { :unfinished => true } task: ----- belongs_to :project Thanks for any help with this! Tom -- Posted via http://www.ruby-forum.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-/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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-06 17:32 UTC
Re: Is there a way to define "sub models" in Rails?
On 6 Apr 2008, at 18:29, Tom Ha wrote:> > My question: > > Is there a way in Rails to define some sort of a "sub model", like > this? > (And if yes, how exactly?) > > project: > -------- > has_many :tasks > has_many :unfinished_tasks, :class => ''task'', :conditions => { > :unfinished => true } >Apart from the fact that the option for specifying the class name is :class_name and not :class, what you''ve got there should already work. Fred> task: > ----- > belongs_to :project > > > Thanks for any help with this! > Tom > -- > Posted via http://www.ruby-forum.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-/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 -~----------~----~----~----~------~----~------~--~---
Oh, very cool, thanks Fred! -- Posted via http://www.ruby-forum.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-/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 -~----------~----~----~----~------~----~------~--~---
On Sun, Apr 06, 2008 at 07:29:59PM +0200, Tom Ha wrote:> Consider the following 2 initial models: > =======================================> project: > -------- > has_many :tasks[...]> My question: > > Is there a way in Rails to define some sort of a "sub model", like this? > (And if yes, how exactly?) > > project: > -------- > has_many :tasks > has_many :unfinished_tasks, :class => ''task'', :conditions => { > :unfinished => true }[...] I generally like to define methods on the has_many collection for that sort of thing: has_many :tasks do def unfinished select(&:unfinished?) end end This allows you to call @project.tasks.unfinished to get the collection of unfinished tasks. That said, if you are trying to avoid retrieving or instantiating the finished tasks, use the other approach with the conditions clause. This approach requires loading (or having loaded) the entire tasks collection.> Thanks for any help with this! > Tom--Greg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-Apr-08 01:07 UTC
Re: Is there a way to define "sub models" in Rails?
It should be :class_name => "Task" and not :class_name => "task". If you left it lower case you''d get Rails whinging that it can''t find a local variable called "task", which can lead to a lot of wtf''ing before figuring that out. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---