hi i have a question about relationships. if i have the folowing classes/tables: project --1--------------n-- iteration iteration has a attribut "start_date". now i like to get all projects whit at least one iteration where start_date = today. for other calculatings i need all iterations related to this projects. how can i do this whit activerecord? thanks for any hints! -- 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 -~----------~----~----~----~------~----~------~--~---
From memory something like this: class Project < ActiveRecord::Base has_many :iterations do def todays_it( today ) find :all, :conditions => [ ''start_date = ?'', today ] end end end class Iteration < ActiveRecord::Base belongs_to :project end Now you can do the following all_projects_iterations = Project.find(:all).iterations.todays_it(today) -- 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 -~----------~----~----~----~------~----~------~--~---
thanks for your suggestion. Peter wrote:> all_projects_iterations = Project.find(:all).iterations.todays_it(today)this can''t work, because Project.find(:all) returns a array and this dosn''t know the method iterations. i''m not sure if this should happens by your code, but i need in the end a array filed whit Project-objects whit all relatet Iterations - if it is possible whit no extra query. know someone a documentation for has_many whit a block given? there is no word in the rails api about this. -- 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 -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-10 17:25 UTC
Re: ActiveRecord relationship
On Dec 8, 5:53 pm, den <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> hi > > i have a question about relationships. if i have the folowing > classes/tables: > > project --1--------------n-- iteration > > iteration has a attribut "start_date". now i like to get all projects > whit at least one iteration where start_date = today. for other > calculatings i need all iterations related to this projects. > > how can i do this whit activerecord? >something like this? class Iteration < ActiveRecord::Base belongs_to :project end class Project < ActiveRecord::Base has_many :iterations def self.find_all_with_iteration_start_date (d) Iteration.find_all_by_start_date(d).collect { |i| i.project } end end projects = Project.find_all_with_iteration_start_date (today)> thanks for any hints! > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
> class Iteration < ActiveRecord::Base > belongs_to :project > end > > class Project < ActiveRecord::Base > has_many :iterations > > def self.find_all_with_iteration_start_date (d) > Iteration.find_all_by_start_date(d).collect { |i| i.project } > end > end > > projects = Project.find_all_with_iteration_start_date (today)Could this be refactored: projects = Iteration.find_all_by_start_date(today).collect { |i| i.project } -- 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 -~----------~----~----~----~------~----~------~--~---
ryan.raaum-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-11 17:56 UTC
Re: ActiveRecord relationship
> Could this be refactored: > > projects = Iteration.find_all_by_start_date(today).collect { |i| > i.project }I''m not sure it''s a "refactoring" - it''s exactly the same thing. The original poster seemed to imply that he conceptually wanted the method to be accessible through the Project model, so that''s why I suggested the convenience method I did. -r> > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
thanks to for all your help. the solution from ryan works great and is easy to use. now i just have to understand in detail why it works. ;) -- 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 -~----------~----~----~----~------~----~------~--~---