# Array help

**URL:** <https://www.ruby-forum.com/t/array-help/181861>\
**Category:** Rails\
**Created:** [January 19, 2010, 11:18am UTC](https://www.ruby-forum.com/t/array-help/181861 "2010-01-19T11:18:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 19, 2010, 11:18am UTC](https://www.ruby-forum.com/t/array-help/181861/1 "2010-01-19T11:18:29Z")

</div>

Hi…

@list.each do |f|

```
    chatfile = ChatfileInfo.find_all_by_id(f.chat_id)

```

end

Is it possible to simplyfy the array iteration in one line…

Thanks

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 19, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/array-help/181861/2 "2010-01-19T12:14:03Z")

</div>

2010/1/19 Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi…
> 
> @list.each do |f|
> 
> Â Â Â Â chatfile = ChatfileInfo.find\_all\_by\_id(f.chat\_id)
> 
> end
> 
> Is it possible to simplyfy the array iteration in one line…

@list.each { |f| chatfile = ChatfileInfo.find\_all\_by\_id(f.chat\_id) }

There is not much point, whether one line or several, though as at the  
end chatfile will be set to the last one so this would be better

chatfile = ChatfileInfo.find\_all\_by\_id(@list.last.chat\_id) if @list.last

and what is this find\_all\_by\_id anyway, should that be find\_by\_id?

Colin

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 19, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/array-help/181861/3 "2010-01-19T15:10:50Z")

</div>

On Jan 19, 2010, at 6:12 AM, Colin L. wrote:

> Colin  
> Well, guessing a bit about what you mean:

Given a list of things having a chat\_id, find all the ChatfileInfo  
records that correspond. And building on Colin’s question, I’m  
assuming that the ChatfileInfo#id is the primary key.

chatfile = ChatfileInfo.find(@list.map(&:chat\_id))

Now this might not be anything close to what you really want. If your  
entire loop is represented by your original post, then Colin’s  
simplification is right.

What are you _really_ trying to do?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [January 21, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/array-help/181861/4 "2010-01-21T15:05:11Z")

</div>

Hi  
Try to apply the proper model relation ship.That will be more easier

Sijo
