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 support for singleton resources?

#2608

ActiveResource find(:one) method returns nil when no :from option is passed.

This contradicts the documentation which presents :one and :from as two separate and complementary things.

We have some singleton resources, and we configure the client dynamically using resource introspection, therefore some resources have no fixed url.

If we configure ActiveResource correctly with 'site' and 'collection_name' we have no problem accessing any resource. However if the resource is a singleton, the following surprise elements appear:

One expects Foo.find(:one) to get /foo.xml without the need to hardcode a :from.

One also expects Foo.save and destroy to act on /foo and not on /foos.

I would like to contribute a patch. However the fix for find(:one) can be done by just extending the case statement in find_one, however a complete support would require making ActiveResource aware that the resource is a singleton. This could be done using a singleton flag, but I am not sure if it is the right solution.

However, the current implementation is confusing, as rails supports excellent support on the controller side for singleton resources, but the integration with the client side is not straightforward.

Reported by Duncan Mac-Vicar · May 5th, 2009 @ 09:50 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Duncan Mac-Vicar
    Duncan Mac-Vicar

    One workaround that worked for me was to redefine element_path and collection_path to not include the id. THis solution was proposed as a workaround in some mailing list by people having the same problem.

    def self.element_path(id, prefix_options = {}, query_options = nil)

    prefix_options, query_options = split_options(prefix_options) if query_options.nil?
    # original: "#{prefix(prefix_options)}#{collection_name}/#{id}.#{format.extension}#{query_string(query_options)}"
    "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
    
    

    end

    collection_name singular def self.collection_path(prefix_options = {}, query_options = nil) prefix_options, query_options = split_options(prefix_options) if query_options.nil? # original: "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}" "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}" end

    And then fix singularize collection_name.

    Thanks to the behavior of ActiveResource, where for a singular resource Foo.find would go to /foos/.xml (because no :id) the workaround above makes find with no arguments to use /foo.xml and the result works fine with save and destroy. find(:all) does not work but this is expected.

    May be a good improvement for ActiveResource would be to have a flag for singular resources and automatically use those element_path and collection_path in those cases.

    I could contribute a patch for that, but I would like to know if the solution is ok.

    May 7th, 2009 @ 11:07 PM

  2. josh
    josh
    • Assigned user cleared.

    May 17th, 2009 @ 04:50 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from 2.3, activeres, activeresource, active_resource, client, singleton to 23, activeres, activeresource, active_resource, client, singleton
    • Importance changed from to

    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 @ 05:02 PM

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

    February 2nd, 2011 @ 05:02 PM