{"id":2801,"date":"2014-02-17T00:15:14","date_gmt":"2014-02-17T00:15:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/17\/how-can-i-use-scopes-with-ransack-in-rails-3-collection-of-common-programming-errors\/"},"modified":"2014-02-17T00:15:14","modified_gmt":"2014-02-17T00:15:14","slug":"how-can-i-use-scopes-with-ransack-in-rails-3-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/02\/17\/how-can-i-use-scopes-with-ransack-in-rails-3-collection-of-common-programming-errors\/","title":{"rendered":"How can I use scopes with Ransack in Rails 3?-Collection of common programming errors"},"content":{"rendered":"<p>In my Widget model I have the following:<\/p>\n<pre><code>scope :accessible_to, lambda { |user|\n  if user.has_role?('admin')\n    self.all\n  else\n    roles = user.roles\n    role_ids = []\n    roles.each { |r| role_ids  role_ids)\n   end\n}\n<\/code><\/pre>\n<p>Ideally, I would like to use this scope as a filter for Ransack&#8217;s search results, so in my controller I have:<\/p>\n<pre><code>def index\n  @q = Widget.accessible_to(current_user).search(params[:q])\n  @widgets = @q.result.order('created_at DESC')\nend\n<\/code><\/pre>\n<p>Doing this generates the following error:<\/p>\n<blockquote>\n<p>undefined method `search&#8217; for Array:0x007ff9b87a0300<\/p>\n<\/blockquote>\n<p>I&#8217;m guessing that Ransack is looking for an ActiveRecord relation object and not an array. Is there anyway that I can use my scope as a filter for Ransack?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my Widget model I have the following: scope :accessible_to, lambda { |user| if user.has_role?(&#8216;admin&#8217;) self.all else roles = user.roles role_ids = [] roles.each { |r| role_ids role_ids) end } Ideally, I would like to use this scope as a filter for Ransack&#8217;s search results, so in my controller I have: def index @q = [&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-2801","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2801","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=2801"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2801\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}