Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource should use to_param to generate routes

#1716

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!

Reported by Matthew Moore · January 9th, 2009 @ 09:06 AM

State: stale
Milestone: 3.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Matthew Moore
    Matthew Moore

    This actually only applies to ActiveResource::CustomMethods ActiveResource already uses to_param

    January 10th, 2009 @ 10:52 PM

  2. Ryan Bigg
    Ryan Bigg
    • 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.

    April 11th, 2010 @ 10:46 PM

  3. Ryan Bigg
  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Dan Pickett
    Dan Pickett
    • Tag changed from activeresource to activeresource, bugmash

    May 9th, 2010 @ 06:14 PM

  6. Daniel Cadenas
    Daniel Cadenas

    +1 verified

    It applied cleanly to master and 2-3-stable and tests pass.

    May 15th, 2010 @ 10:36 PM

  7. Santiago Pastorino
    Santiago Pastorino

    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.

    February 2nd, 2011 @ 04:50 PM

  8. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:50 PM

  9. bingbing