# Array Manupulation

**URL:** <https://www.ruby-forum.com/t/array-manupulation/174243>\
**Category:** Rails\
**Created:** [August 23, 2009, 7:16am UTC](https://www.ruby-forum.com/t/array-manupulation/174243 "2009-08-23T07:16:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pakothawade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba9def/32.png) [@pakothawade](https://www.ruby-forum.com/u/pakothawade)\
**Post date:** [August 23, 2009, 7:16am UTC](https://www.ruby-forum.com/t/array-manupulation/174243/1 "2009-08-23T07:16:52Z")

</div>

Can anyone help me on the following?

I have the following 2 arrays, how do i get c as a result of a and b.  
The elements should be in the specified order.

a = ["(modulable\_type=?) AND (parent\_id=?) AND  
(status=?)",“Task”,“2”,“20”]  
b = ["(tasks.due\_date=?)", “2009-08-22”]

c = ["(modulable\_type=?) AND (parent\_id=?) AND (status=?) AND  
(tasks.due\_date=?)",“Task”,“2”,“20”,“2009-08-22”]

Thanks and Regards,  
Pratik

---

<div class="post-metadata">

**Author:** ![pakothawade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba9def/32.png) [@pakothawade](https://www.ruby-forum.com/u/pakothawade)\
**Post date:** [August 23, 2009, 8:45am UTC](https://www.ruby-forum.com/t/array-manupulation/174243/2 "2009-08-23T08:45:51Z")

</div>

# Answer:

a[0] = a[0] + " AND " + b[0]

```
  b.length.times do |i|
    if i!=0
a << b[i]

```

end  
end

---

<div class="post-metadata">

**Author:** ![pakothawade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba9def/32.png) [@pakothawade](https://www.ruby-forum.com/u/pakothawade)\
**Post date:** [August 23, 2009, 10:24am UTC](https://www.ruby-forum.com/t/array-manupulation/174243/3 "2009-08-23T10:24:45Z")

</div>

2009/8/23 tispratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> c = [“(modulable\_type=?) AND (parent\_id=?) AND (status=?) AND  
> (tasks.due\_date=?)”,“Task”,“2”,“20”,“2009-08-22”]

I have encountered exactly this problem and realised that, though this  
is solvable, it is better (if possible) to build the arrays  
differently in the first place. I realise this may not be possible  
for you but if you change the original conditions building so that it  
builds two arrays of the form  
conditions = [“(modulable\_type=?)”,“(parent\_id=?)”]  
and  
values = [“Task”,“2”] and so on, so just adding each condition and  
value as they arise, then the required result is  
c = [conditions.join(" AND ")] + values

No doubt there are even better ways, and it may not match your needs  
anyway, but for me it worked well as I was working through a number of  
options building the conditions as I went.

Colin

and b similarly  
Then c = (a+b).collect(

---

<div class="post-metadata">

**Author:** ![pakothawade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba9def/32.png) [@pakothawade](https://www.ruby-forum.com/u/pakothawade)\
**Post date:** [August 23, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/array-manupulation/174243/4 "2009-08-23T19:22:02Z")

</div>

On Aug 23, 1:15 am, tispratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (tasks.due\_date=?)",“Task”,“2”,“20”,“2009-08-22”]
> 
> Thanks and Regards,  
> Pratik

Instead of doing funky array manipulation to build your model’s SQL  
find conditions, are you absolutely sure you shouldn’t just be using  
scopes instead?

---

<div class="post-metadata">

**Author:** ![pakothawade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba9def/32.png) [@pakothawade](https://www.ruby-forum.com/u/pakothawade)\
**Post date:** [August 23, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/array-manupulation/174243/5 "2009-08-23T12:55:45Z")

</div>

Doing things like this, it may be easier to use the named placeholders  
version - so your example will look like:

a = ["(modulable\_type=:modulable\_type) AND (parent\_id=:parent\_id) AND  
(status=:status)",{:modulable\_type =\> “Task”, :parent\_id =\>  
“2”, :status =\> “20”}]  
b = ["(tasks.due\_date=:due\_date)", {:due\_date =\> “2009-08-22”}]

Then c is as simple as:

c = ["#{a[0]} AND #{b[0]}", a[1].merge(b[1])]

Note: this will fail in creative ways if you have parameters in a and  
b that are supposed to have different values but have the same name,  
so don’t do that. 🙂

–Matt J.

---

<div class="post-metadata">

**Author:** ![pakothawade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ba9def/32.png) [@pakothawade](https://www.ruby-forum.com/u/pakothawade)\
**Post date:** [August 24, 2009, 4:20am UTC](https://www.ruby-forum.com/t/array-manupulation/174243/6 "2009-08-24T04:20:13Z")

</div>

pharrington wrote:

> On Aug 23, 1:15ï¿½am, tispratik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > (tasks.due\_date=?)",“Task”,“2”,“20”,“2009-08-22”]
> > 
> > Thanks and Regards,  
> > Pratik
> 
> Instead of doing funky array manipulation to build your model’s SQL  
> find conditions, are you absolutely sure you shouldn’t just be using  
> scopes instead?

Or if not scopes, plain old conditions hashes. If you’re trying to use  
this as a :conditions parameter to find, remember that :conditions will  
also take a hash:

## [“(modulable\_type=?) AND (parent\_id=?) AND (status=?) AND (tasks.due\_date=?)”,“Task”,“2”,“20”,“2009-08-22”] {:modulable\_type =\> ‘Task’, :parent\_id =\> 2 … } Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
