Topic: act_as_ferret Plugin

Hi all

I was wondering if we could have a conditional search using the act_as_ferret plugin
The case that I am looking forward to is an admin can search the user under him by using the first, last or the company name. The catch is that he should only search for the user under him and not for the user that are under any other admin.
           The user table has an admin_id field that matches it with an admin.

class User < ActiveRecord::Base
  acts_as_ferret :fields => [:first_name, :last_name, :company]
end

Now how can I modify the code below
@results = User.find_by_contents("Gregg")

To include a condition that would check for the admin_id..
Some thing in the lines of
@results = User.find_by_contents("Gregg",:conditions => ['admin_id = ?', params[:admin_id]])

Any help in this regard would be highly appreciated.

Re: act_as_ferret Plugin

Just a wild stab in the dark, but try this inside your search method

if admin?
  @results = User.find_all_by_admin_id(params[current_user.id])
else
  #put your regular search code here
end

This assumes you have an admin? method to check the current user's status.

def admin?
      if logged_in? && current_user.type == 'admin'
        return true
      end
    end
# Note: I am using STI for users, hence the :type attribute..use whatever field you have to determine user roles instead

Re: act_as_ferret Plugin

Hi Vinay,

what you do looks about right. However, try this little modification:

@results = User.find_by_contents("Gregg", {}, { :conditions => ['admin_id = ?', params[:admin_id]]})

If you examine the find_by_contents method in the AAF plugin, you'll notice the find_options are used to filter the ferret results, e.g. filter all users with admin_id.

Hope that helps.

Rudi