# Can Two Active Record Results be Added?

**URL:** <https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996>\
**Category:** Rails\
**Created:** [July 24, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996 "2008-07-24T15:10:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/1 "2008-07-24T15:10:21Z")

</div>

I have an sql with some inputs,notably a date. For that date I run  
the sql and get a result

@result = Model.find\_by\_sql(sql)

What I want is to have eight versions of this (different dates in the  
sql) and then combine all the results and pass them back once to the  
view. Is that possible?

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/2 "2008-07-24T16:22:39Z")

</div>

On Jul 24, 8:09 am, Jorg L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an sql with some inputs,notably a date. For that date I run  
> the sql and get a result
> 
> @result = Model.find\_by\_sql(sql)
> 
> What I want is to have eight versions of this (different dates in the  
> sql) and then combine all the results and pass them back once to the  
> view. Is that possible?

Yes it is

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 7:13pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/3 "2008-07-24T19:13:48Z")

</div>

If you can come up w/SQL that will return \> 1 record, find\_by\_sql (like  
all the other find\* methods) should return an array of instances of your  
model w/out any extra work on your part.

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/4 "2008-07-24T19:49:34Z")

</div>

Huh–can you not pass a “exec my\_cool\_sproc” to find\_by\_sql?

But if you’ve just got a set of dates to cycle through, can’t you just  
stash those in an in() list? In fact, I wouldn’t be surprised if you  
could just pass an array of DateTimes to Model.find\_by\_my\_date\_field. I  
know the plain Model.find will take an array of IDs…

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/5 "2008-07-24T19:21:24Z")

</div>

In this case that would be a lot harder than creating the instances  
multiple times and adding them together. Though it’s likely more  
efficient to do it all on the backend, doing procedural logic in SQL  
isn’t very clean. If ActiveRecord supported stored procs it would  
make it easier. Of course some people think all logic, even data  
logic,belongs outside the data layer.

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/6 "2008-07-24T20:41:48Z")

</div>

exec doesn’t work in DB2. I can pass a call. That seems to work but  
not if there’s output parms “uncaught throw `Column information cannot  
be retrieved:” Something to explore a little later. One could also  
write a UDF.

An in list would work the way you describe but in this case it’s  
basically a concatenation of the sql statement with each different  
date. On 7/28 5 employees might be on PTO on 8/4 maybe 7. Each date  
needs to be tested.

---

<div class="post-metadata">

**Author:** ![Jorg\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/76d3ee/32.png) [@Jorg\_L](https://www.ruby-forum.com/u/Jorg_L)\
**Post date:** [July 24, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/can-two-active-record-results-be-added/143996/7 "2008-07-24T21:04:41Z")

</div>

Ahhh–okay. I get it now. Well, at least the ruby isn’t too painful to  
write–something like:

def self.find\_unavailable\_by\_dates(array\_of\_dates)  
results = []  
array\_of\_dates.uniq!.sort!  
array\_of\_dates.each do |this\_date|  
sql = “blah blah blah #{this\_date}”  
results \<\< self.find\_by\_sql(sql)  
end  
results  
end

Should do it I’d think…
