先に注意してください。私は合計RoR n00bですが、私はプログラミングの経験があるので、私は基本的なのを取得します。私は複雑な検索エンジンを構築するために必要なアプリケーションを用意しています。基本的なレイアウトは、ガイド>>プロファイル>>メンターリング>>メンターエリアです。以下は、それぞれのモデルのコードと、次にビルドしようとしているコードです。私の問題は、私は適切なオブジェクト名を検索して、検索エンジンにmentor_areasを検索させることができないようです。RanSack複合オブジェクトの関係が構築されない...(メソッドが見つかりませんでしたエラー)
システムのセットアップ:
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に対して検索するように、2番目のフィールドの正しい名前が何であるべきかを理解できないようです。
ありがとうございます!掻き回すコード
いや、残念ながらそれはどちらか動作しません。私は何かが私のモデルで正しく設定されていないのだろうかと思っています。私もmeta_searchの代わりにrails3.1とされているransackを試しました。まだ愛はありません。 – witharmshigh