0

First come caveats. I'm a total RoR n00b but i have experience with programming so i get the basic's. I've got an application i'm building which i need to build a complex search engine for. The basic layout is Guides >> Profiles >> Mentorings >> MentorAreas. Below is the code for each of the models and then the code i'm trying to build. My issue is i can't seem to figure out the proper object name to get the search engine to search mentor_areas.

System Setup:

rails -v :: Rails 3.1.1
ruby -v :: ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
RanSack :: ransack (0.5.8) from git://github.com/ernie/ransack.git (at master) 

Guide:

class Guide < User
end

User: (what's relevant)

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Virtual attribute for authenticating by either username or email
  # This is in addition to a real persisted field like 'username'
  attr_accessor :login

  # Setup accessible (or protected) attributes for your model
  attr_accessible :login, :username, :email, :password, :password_confirmation, :remember_me, :sex, 
    :location, :role, :first_name, :last_name, :home_town, :profile_attributes

  has_one :profile, :dependent => :destroy
  accepts_nested_attributes_for :profile, :allow_destroy => true

  has_and_belongs_to_many :roles

end

Profile

class Profile < ActiveRecord::Base
  belongs_to :user
  has_many :mentor_areas, :through => :mentorings
  has_many :mentorings

  accepts_nested_attributes_for :mentor_areas, 
     :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }, :allow_destroy => true

  validates_uniqueness_of  :user_id
end

Mentoring

class Mentoring < ActiveRecord::Base
   belongs_to :mentor_area
   belongs_to :profile

   validates_uniqueness_of  :profile_id, :scope => :mentor_area_id
end

MentorArea

class MentorArea < ActiveRecord::Base
  has_many :profiles, :through => :mentorings
  has_many :mentorings

  validates_uniqueness_of  :mentor_area
end

In my Guides Controller i have:

@search_guides = Guide.joins(:roles).where("sex = :sex AND roles.name = :role",{:sex => current_user.sex, :role => 'guide'}).search(params[:search])
@guides_found = @search_guides.all 

and in my view (index.html.erb) i have the following:

<%= form_for @search_guides do |f| %>
    <%= f.label :username_cont %>
    <%= f.text_field :username_cont %><br />
    <%= f.label :guides_profiles_mentor_areas_mentor_area_cont %>
    <%= f.text_field :guides_profiles_mentor_areas_mentor_area_cont %><br />
    <%= f.submit %>
<% end %>

I can't seem to figure out what the correct name should be for the second field so that it will search against the mentor_areas that a person has associated with there profile.

thanks in advance!

Updated for RanSack Code

Baylor Rae'
  • 3,995
  • 20
  • 39
witharmshigh
  • 103
  • 1
  • 8

2 Answers2

5

If I'm reading your code correctly, you want:

:profile_mentor_areas_mentor_area_cont
Ernie
  • 896
  • 5
  • 7
  • something else must be wrong because that's what i thought too but when i do that i get 'undefined method "profile_mentor_areas_mentor_area_conts" for #' – witharmshigh Oct 28 '11 at 16:26
  • Hi Ernie, I have a little config. chore using ransack, Could you please have a look at my question: http://stackoverflow.com/questions/13369909/how-to-set-defaults-for-ransack-sorting I think this should be easy enough to fix (maybe) :) Thanks in advance. – jlstr Nov 14 '12 at 15:00
0

I think it's:

:profiles_mentorings_mentor_area_mentor_area_contains

Basically, it's you have to think about which tables you have to reach through one by one in sequence to get to your search field. I think in your case, your view for guides needs to go through : profiles -> mentoring -> mentor_area in order to search for the mentor area? Your mentor_area only have profiles :through the mentoring model. You can't go directly to the mentor_are without going through the mentoring model first.

Also,

<%= f.label :guides_profiles_mentor_areas_mentor_area_contains %>

looks really cumbersome in the view. You can just say this instead:

<%= f.label "Mentor Area" %>

Hope that works.

noob
  • 1,807
  • 2
  • 18
  • 34
  • yea unfortunately that doesn't work either. i'm wondering if something is not setup right in my models. i've also tried ransack which is supposed to be the rails3.1 replacement for meta_search. still no love. – witharmshigh Oct 26 '11 at 18:26