# Polymorphic\_path - How to Create One given a table

**URL:** <https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872>\
**Category:** Rails\
**Created:** [October 1, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872 "2010-10-01T17:43:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 1, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872/1 "2010-10-01T17:43:46Z")

</div>

Hello, i have table AuditLog with fields including: audited\_id |  
audited\_type

That results in data like:  
108 | Photo  
303 | Comment

What I want to do is create a link to the item, so for the example  
above:

[here is the photo](https://www.ruby-forum.com/photos/108)

I’m trying to use a polymorphic\_path but am getting an error:  
“undefined method `model\_name’ for Fixnum:Class”

When using:

```
<%= link_to 'Here she is', polymorphic_path([audited_id,

```

audited\_type]) %\>

Ideas? Thanks

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 1, 2010, 7:33pm UTC](https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872/2 "2010-10-01T19:33:51Z")

</div>

On Fri, Oct 1, 2010 at 11:42 AM, nobosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [here is the photo](https://www.ruby-forum.com/photos/108)
> 
> I’m trying to use a polymorphic\_path but am getting an error:  
> “undefined method `model\_name’ for Fixnum:Class”
> 
> When using:
> 
> \<%= link\_to ‘Here she is’, polymorphic\_path([audited\_id,  
> audited\_type]) %\>
> 
> you have to pass 2 objects to polymorphic\_path(parent\_model\_object,  
> object) , audited\_id is a number , that is what Fixnum:Class means,  
> polymorphic path assembles a helper method like this,

```
   polumorphic_path(audit.auditable, audit ) =====> takes the first

```

object passed as audit.auditable, because auditable is the alias for the  
parent model, so in your case is trying to get the model out of that  
first  
parameter which is a number, when it should be instance of Photo or  
comment,  
so it can create this =\> photo\_audit\_path , you see? this happens if  
you  
pass an instance of the parent model and then the instance of the  
polymorphic model.

you are passing a number and a string, auditer\_id is a number and  
audited\_type is a string.

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 1, 2010, 8:16pm UTC](https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872/3 "2010-10-01T20:16:35Z")

</div>

Interesting, so what I have available is:  
audited\_id = 33  
audited\_type = Photo

So using the polymorphic\_path how do I take what I have from the DB  
AuditLog record and pass an instance of Photo?

\<%= link\_to ‘Click to See It!’, polymorphic\_path([record.audited\_id,  
record.audited\_type]) %\>

?

Maybe there is a better way to make the link : “/photos/33” based on  
the data I have?

Something like /\<%audited\_type.pluralize%\>/\<%audited\_id%\>

Any suggestions? Or is polymorphic\_path the right way to go?

Thanks,

Brett

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 1, 2010, 11:08pm UTC](https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872/4 "2010-10-01T23:08:40Z")

</div>

Ah shoot, I think the reason it wasn’t working is I had some records  
in the table that were empty for the polymorphic assoc… I guess it  
fails if it’s blank… thank you!

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [October 1, 2010, 9:05pm UTC](https://www.ruby-forum.com/t/polymorphic-path-how-to-create-one-given-a-table/195872/5 "2010-10-01T21:05:45Z")

</div>

On Fri, Oct 1, 2010 at 2:15 PM, nobosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Interesting, so what I have available is:  
> audited\_id = 33  
> audited\_type = Photo
> 
> So using the polymorphic\_path how do I take what I have from the DB  
> AuditLog record and pass an instance of Photo?

hum , i thought i was clear… oh well

polymorphic\_path(audit.auditable, audit )
