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.find :from won't fill prefix_options on loaded resources

#4481

I have a problem, the ActiveResource.find method normally will fill the prefix_options on loaded resources, so the loaded resources will be able to do something with them, while refering to the correct URL.

But when I used the :from option, to be able to use a RESTFUL action on a resource, the loaded resources didn't have those prefix_options filled, so I needed to do that by hand, or by code!

class Call < ActiveResource::Base
self.site = "http://localhost:3000/accounts/:account_id/" end

calls = Call.find(:all, :params => { account_id => 1})
call = calls.first
call.state = "NEW_STATE"
call.save -> Give 200 OK
path generated will be = "http://localhost:3000/accounts/1/calls.xml"

calls = Call.find(:all, :from => :available, :params => { account_id => })
.... call.save -> Give 404 status

path generated will be = "http://localhost:3000/accounts//calls.xml"

What I did was:

calls.each do |call|
call.prefix_options[:account_id => 1] end

And that worked, but I don't think that diference in behavior is fine when find is used with :from.

Thanks in advance,
Juan.

Reported by Juan · April 26th, 2010 @ 09:54 PM

State: stale
Milestone: none
Assigned to: Ryan Bigg Ryan Bigg
Importance: Low

Activity

  1. Ryan Bigg
    Ryan Bigg
    • State changed from new to incomplete

    Please create a failing test case for this issue.

    April 27th, 2010 @ 12:46 AM

  2. Juan
    Juan

    I think I found another incorrect behavior, when an AR item returned from find or find :form gets saved it looses it's prefix_options, so if you save it, and after that you want to do anything with that object the url will be malformed.

    Both behaviors are in the test case attached.

    April 27th, 2010 @ 09:34 PM

  3. Juan
    Juan
    • Assigned user set to Ryan Bigg

    May 17th, 2010 @ 05:30 AM

  4. Neeraj Singh
    Neeraj Singh

    Hi Juan,

    I'm sorry but I'm unable to follow you.

    As per you

    Call.find(:all, :params => { account_id => 1})
    

    is generating

    "http://localhost:3000/accounts/1/calls.xml"

    And

    Call.find(:all, :from => :available, :params => { account_id => })
    

    is generating

    "http://localhost:3000/accounts//calls.xml"

    So what's the issue here?

    May 18th, 2010 @ 03:08 AM

  5. Juan
    Juan

    The generated url for the resources = Call.find :all is OK in both calls.

    The problem is when using :from => :method the returned resources when you do resource.save will produce that incomplete and invalid url.

    Looking at the activeresource source I found that in some cases, including the :from method the resources loaded looses the prefix_options.

    This is ok:
    resources = Call.find(:all, :params => { :account_id => 1})
    resources[0].save => produced a valid URL

    This is not ok:
    resources = Call.find(:all, :from => :method, :params => { :account_id => 1})
    resources[0].save => produced an invalid URL -> returned resource looses the prefix_options[:account_id] that could make up the correct URL.

    I hope I could clarify a little the I think incorrect behavior.

    Juan.

    May 18th, 2010 @ 09:16 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open
    • Tag changed from :from, activeresource to activeresource, from
    • Importance changed from to Low

    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:38 PM

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

    February 2nd, 2011 @ 04:38 PM