{"id":2621,"date":"2022-08-30T15:26:23","date_gmt":"2022-08-30T15:26:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/activeresource-with-conditional-prefix-self-prefix-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:23","modified_gmt":"2022-08-30T15:26:23","slug":"activeresource-with-conditional-prefix-self-prefix-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/activeresource-with-conditional-prefix-self-prefix-collection-of-common-programming-errors\/","title":{"rendered":"ActiveResource with conditional prefix (self.prefix)?-Collection of common programming errors"},"content":{"rendered":"<p>Using ActiveResource and when I have a nested resource, using the &#8216;prefix&#8217; works great.<\/p>\n<pre><code>class Account &lt; ActiveResource::Base\n  self.prefix = \"\/users\/:user_id\/\"\nend\n<\/code><\/pre>\n<p>All is fine as long as :user_id has been defined, or else an error will be raised. <strong>But how to make the &#8216;self.prefix&#8217; conditional, in cases where I don&#8217;t want to access this resource as a nested resource, but rather as the resource itself?<\/strong> For example, I&#8217;d like to retrieve all accounts, not just the accounts scoped by a particular user?<\/p>\n<ol>\n<li>\n<p>You could set the prefix to be entirely dynamic:<\/p>\n<pre><code>class Account &lt; ActiveResource::Base\n    self.prefix = \":prefix_path\"\nend\n<\/code><\/pre>\n<p>Then set it at runtime:<\/p>\n<pre><code>Account.find(:all, :params =&gt; { :prefix_path =&gt; '\/users\/4' } )\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 09:41:13. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Using ActiveResource and when I have a nested resource, using the &#8216;prefix&#8217; works great. class Account &lt; ActiveResource::Base self.prefix = &#8220;\/users\/:user_id\/&#8221; end All is fine as long as :user_id has been defined, or else an error will be raised. But how to make the &#8216;self.prefix&#8217; conditional, in cases where I don&#8217;t want to access this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2621","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2621"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2621\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}