Array help

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

@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

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 Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

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

Sijo