This project is archived and is in readonly mode.
ActiveResource support for singleton resources?
Reported by Duncan Mac-Vicar | May 5th, 2009 @ 09:50 AM | in 3.x
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.
Comments and changes to this ticket
-
Duncan Mac-Vicar May 7th, 2009 @ 11:07 PM
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.
-
josh May 17th, 2009 @ 04:50 PM
- Assigned user cleared.
-
Santiago Pastorino February 2nd, 2011 @ 05:02 PM
- 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.
-
Santiago Pastorino February 2nd, 2011 @ 05:02 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>