# polymorphic reference deeper ?

**URL:** <https://discuss.rubyonrails.org/t/polymorphic-reference-deeper/59074>\
**Category:** rubyonrails-talk\
**Created:** [May 20, 2011, 3:48pm UTC](https://discuss.rubyonrails.org/t/polymorphic-reference-deeper/59074 "2011-05-20T15:48:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niklas\_Nson1](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@Niklas\_Nson1](https://discuss.rubyonrails.org/u/Niklas_Nson1)\
**Post date:** [May 20, 2011, 3:48pm UTC](https://discuss.rubyonrails.org/t/polymorphic-reference-deeper/59074/1 "2011-05-20T15:48:06Z")

</div>

I have made myself a small activity model like this =\>

# == Schema Information # Schema version: 20110519174324

---

<div class="post-metadata">

**Author:** ![Matt\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@Matt\_Jones](https://discuss.rubyonrails.org/u/Matt_Jones)\
**Post date:** [May 21, 2011, 3:00pm UTC](https://discuss.rubyonrails.org/t/polymorphic-reference-deeper/59074/2 "2011-05-21T15:00:31Z")

</div>

> I have made myself a small activity model like this =\>
> 
> # == Schema Information # Schema version: 20110519174324 # # Table name: activities # # id :integer not null, primary key # account\_id :integer # action :string(255) # item\_id :integer # item\_type :string(255) # created\_at :datetime # updated\_at :datetime
> 
> belongs\_to :account belongs\_to :item, :polymorphic =\> true
> 
> from this i can get a nice timeline/actions/userfeed or what we want to call it from =\>
> 
> def self.get\_items(account) item\_list = items = where(:account\_id =\> account.id).order("created\_at DESC").limit(30) items.each do |i| item\_list \<\< i.item\_type.constantize.find(i.item\_id)

Quick note here - this can be more effectively replaced by:

item\_list \<\< i.item

since that's the whole point of the :polymorphic =\> true declaration...

> it works like a charm for objects not refering to others - like if a user uploads a new image i can get the image nice and tidy. But for comments =\> then i want to get the original post info, can i get that in get\_items directly ? Someone got tips for me ?

I suspect you'll likely be doing that farther up in the code, where the results of get\_items are used. For instance, one might do something like this:

\<%= Activity.get\_items(some\_account).do |item| %\> &nbsp;&nbsp;\<%= render :partial =\> "#{item.class.name.underscore}", :locals =\> { :item =\> item } %\> \<% end -%\>

then the individual item partials can deal with calling more methods on the object.

--Matt Jones

---

<div class="post-metadata">

**Author:** ![Niklas\_Nson1](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@Niklas\_Nson1](https://discuss.rubyonrails.org/u/Niklas_Nson1)\
**Post date:** [May 22, 2011, 6:36am UTC](https://discuss.rubyonrails.org/t/polymorphic-reference-deeper/59074/3 "2011-05-22T06:36:49Z")

</div>

Thanks Matt,

Now i just have to figure out how to do the "calling more methods on the object". I havent seen or used "#{item.class.name.underscore}" so i guess the key are in that - hopefully i dig into this.

/Niklas.

---

<div class="post-metadata">

**Author:** ![Niklas\_Nson1](https://avatars.discourse-cdn.com/v4/letter/n/90db22/32.png) [@Niklas\_Nson1](https://discuss.rubyonrails.org/u/Niklas_Nson1)\
**Post date:** [May 22, 2011, 6:50am UTC](https://discuss.rubyonrails.org/t/polymorphic-reference-deeper/59074/4 "2011-05-22T06:50:37Z")

</div>

Got it working, you are my hero!
