This project is archived and is in readonly mode.
ActiveResource should use to_param to generate routes
Reported by Matthew Moore | January 9th, 2009 @ 09:06 AM | in 3.x
Let's say you have a resource that uses a permalink to_param, instead of id.
To find this using ActiveResource, you'd usually do something like:
ar = MyActiveResource.new :id => 'my-permalink'
ar.id #=> 'my-permalink'
ar.get(:custom_action)
ar.id #=> 5
However, this causes problems for the next call you do to 'ar', because id will be over-written by the ID of the object, not the permalink.
If you did a subsequent call again (or a save), you'd get a 404, because you're actually sending the ID, not the permalink.
ar.get(:custom_action)
ar.save
Instead, the solution should be to let you set a to_param method in your ActiveRecord class that can be overridden in 'permalink'-style cases. E.g.:
MyActiveResource < ActiveResource::Base
def to_param
self.permalink
end
end
So the following would work:
ar = MyActiveResource.new :permalink => 'my-permalink'
ar.id #=> nil
ar.permalink #=> 'my-permalink'
ar.to_param #=> 'my-permalink'
ar.get(:custom_action)
ar.id #=> 5
ar.permalink #=> 'my-permalink'
ar.to_param #=> 'my-permalink'
ar.get(:custom_action) # now works!
ar.save # now works!
Any thoughts appreciated before I dive in and take the time to submit the patch!
Comments and changes to this ticket
-
Matthew Moore January 10th, 2009 @ 10:52 PM
This actually only applies to ActiveResource::CustomMethods ActiveResource already uses to_param
-
Ryan Bigg April 11th, 2010 @ 10:46 PM
- State changed from new to open
Here's two patches, apply to_param first then to_param2 after. The second patch removes some debug stuff. The third patch applies to Rails 3.
-
Ryan Bigg April 11th, 2010 @ 10:46 PM
- no changes were found...
-
Dan Pickett May 9th, 2010 @ 06:14 PM
- Tag changed from activeresource to activeresource, bugmash
-
Daniel Cadenas May 15th, 2010 @ 10:36 PM
+1 verified
It applied cleanly to master and 2-3-stable and tests pass.
-
Santiago Pastorino February 2nd, 2011 @ 04:50 PM
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 04:50 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>