# Model associations and optimization problem

**URL:** <https://www.ruby-forum.com/t/model-associations-and-optimization-problem/184383>\
**Category:** Rails\
**Created:** [March 1, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/model-associations-and-optimization-problem/184383 "2010-03-01T20:09:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [March 1, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/model-associations-and-optimization-problem/184383/1 "2010-03-01T20:09:41Z")

</div>

Hi.

Any way to write this more efficiently? Can I make the active record  
handle this through some kind of association?

```
@images = []
current_user.foo_models.each do |foo|
  @images.concat(foo.images)
end

```

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [March 1, 2010, 8:21pm UTC](https://www.ruby-forum.com/t/model-associations-and-optimization-problem/184383/2 "2010-03-01T20:21:29Z")

</div>

On Mon, Mar 1, 2010 at 11:09 AM, Michal B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

class FooModel \< ActiveRecord::Base  
belongs\_to :user  
has\_many :images  
end

class User \< ActiveRecord::Base  
has\_many :foo\_models  
has\_many :images, :through =\> :foo\_models  
end

current\_user.images
