0

先来注意事项。我完全是 RoR n00b,但我有编程经验,所以我掌握了基础知识。我有一个正在构建的应用程序,我需要为其构建一个复杂的搜索引擎。基本布局是指南 >> 个人资料 >> 指导 >> MentorAreas。下面是每个模型的代码,然后是我正在尝试构建的代码。我的问题是我似乎无法找出正确的对象名称来让搜索引擎搜索导师区域。

系统设置:

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) 

指导:

class Guide < User
end

用户:(什么是相关的)

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

轮廓

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

指导

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

   validates_uniqueness_of  :profile_id, :scope => :mentor_area_id
end

导师专区

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

  validates_uniqueness_of  :mentor_area
end

在我的指南控制器中,我有:

@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 

在我看来(index.html.erb)我有以下内容:

<%= 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 %>

我似乎无法弄清楚第二个字段的正确名称应该是什么,以便它将搜索一个人与该个人资料相关联的导师区域。

提前致谢!

RanSack 代码更新

4

2 回答 2

5

如果我正确阅读了您的代码,您需要:

:profile_mentor_areas_mentor_area_cont
于 2011-10-27T18:27:05.577 回答
0

我想这是:

:profiles_mentorings_mentor_area_mentor_area_contains

基本上,您必须考虑必须依次通过哪些表才能到达您的搜索字段。我认为在您的情况下,您对指南的看法需要通过:profiles->mentoring->mentor_area 才能搜索导师区域?您的导师区域只有个人资料:通过导师模型。如果不先通过指导模型,您不能直接进入指导者中心。

还,

<%= f.label :guides_profiles_mentor_areas_mentor_area_contains %>

在视图中看起来确实很麻烦。你可以这样说:

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

希望有效。

于 2011-10-19T21:22:23.063 回答