# ActiveRecord relationship

**URL:** <https://www.ruby-forum.com/t/activerecord-relationship/78835>\
**Category:** Rails\
**Created:** [December 8, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835 "2006-12-08T23:53:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 8, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/1 "2006-12-08T23:53:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 9, 2006, 3:23am UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/2 "2006-12-09T03:23:29Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 10, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/3 "2006-12-10T18:27:31Z")

</div>

On Dec 8, 5:53 pm, den [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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)

---

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 11, 2006, 2:28am UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/4 "2006-12-11T02:28:50Z")

</div>

> 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 }

---

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 11, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/5 "2006-12-11T18:57:18Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 10, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/6 "2006-12-10T14:41:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![den](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ce7236/32.png) [@den](https://www.ruby-forum.com/u/den)\
**Post date:** [December 13, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/activerecord-relationship/78835/7 "2006-12-13T12:42:34Z")

</div>

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. 😉
