views:

37

answers:

1

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

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

Updating with code based on the answer by Luke below:

class NewsFeed < ActiveRecord::Base
    default_scope :order => 'news_feeds.created_at DESC'    
    belongs_to :feeded, :polymorphic => true
end

class Note < ActiveRecord::Base
  has_many :newsfeed, :as => :feeded
end

In the partial which is being passed the local storyitem:

<%= link_to 'Here she is', polymorphic_path(storyitem.feeded) %>

The DB migration file, contains the following line for CreateNewsFeeds

  t.references :feeded, :polymorphic => true
+1  A: 

You should have a method #auditable (or whatever your polymorphic association is called) on AuditLog objects. If you pass the result of that method to polymorphic_path it will return the correct path for you.

Update:

Assuming you have the following associations (or are using acts_as_auditable or something that sets up the relationships for you):

class AuditLog
  belongs_to :auditable, :polymorphic => true
end

class AuditedObject
  has_many :audits, :as => :auditable
end

You'll be able to call auditable on any instance of AuditLog, and it will return the associated audited object. So you can call

<%= link_to 'Here she is', polymorphic_path(audit_log.auditable) %>

to get a link to the audited object.

So, anywhere that you have a polymorphic association in a class, there is an instance method setup with the name of that association that will return the associated object.

Gosh, I'm hoping that makes sense. Let me know if you need me to clarify it further.

luke_randall
In case it's not clear, AuditLog#auditable returns the associated object, which is handy since you'd otherwise have to do something like audited_type.constantize.find(audited_id).
luke_randall
@Luke, thanks but I'm not sure I'm following... Is the code example above correct? If not, any way you could update your answer? Also, are you saying I need to make sure I have by belongs_to, has_many relationships setup in the models for the Photos model? thxs
WozPoz
Luke, thanks for explaining this so well. I think I'm getting the general concept now but for some reason I am struggling to get it to work. I updated the question with my specific code. Can you let me know if you see anything that I'm doing wrong?
WozPoz
one more thing, the above gives the error "undefined method `model_name' for NilClass:Class"
WozPoz
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!
WozPoz
Yeah, as you discovered you'll need to do something like <%= link_to 'Here she is', polymorphic_path(storyitem.feeded) if storyitem.feeded %> or else it'll fail where the association is empty. Good luck with your app :)
luke_randall