RanSack 复杂的对象关系未构建...(方法未找到错误)

发布于 2024-12-10 14:19:17 字数 2884 浏览 0 评论 0原文

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

系统设置:

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

MentorArea

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

我似乎无法弄清楚正确的名称应该是什么用于第二个字段,以便它将搜索某人与该个人资料关联的 Mentor_areas。

提前致谢!

更新了 RanSack 代码

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-12-17 14:19:17

如果我正确地阅读你的代码,你想要:

:profile_mentor_areas_mentor_area_cont

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

:profile_mentor_areas_mentor_area_cont
睡美人的小仙女 2024-12-17 14:19:17

我认为是:

:profiles_mentorings_mentor_area_mentor_area_contains

基本上,您必须考虑必须按顺序一一访问哪些表才能到达搜索字段。我认为就您而言,您对指南的看法需要经过:配置文件 ->指导-> Mentor_area 以便搜索导师区域?您的导师区域只有个人资料:通过指导模型。如果不先经过指导模型,您就无法直接进入 Mentor_are。

而且,

<%= f.label :guides_profiles_mentor_areas_mentor_area_contains %>

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

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

希望有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文