Najpierw przychodzą zastrzeżenia. Mam całkowity RoR n00b, ale mam doświadczenie w programowaniu, więc dostaję podstawowe. Mam aplikację, którą buduję, której potrzebuję do zbudowania złożonej wyszukiwarki. Podstawowy układ to Przewodniki >> Profile >> Mentorings >> MentorAreas. Poniżej znajduje się kod każdego z modeli, a następnie kod, który próbuję zbudować. Moim problemem jest to, że nie potrafię znaleźć właściwej nazwy obiektu, aby wyszukiwarka przeszła wyszukiwanie mentor_areas.
Ustawienia systemu:
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)
Przewodnik:
class Guide < User
end
Użytkownik: (co jest istotne)
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
Profil
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
W moim Przewodniku Kontrolera mam:
@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
i moim zdaniem (index.html.erb) mam następujące:
<%= 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 %>
Nie mogę się domyślić, jaka powinna być poprawna nazwa dla drugiego pola, tak aby wyszukujeło na podstawie mentor_areas, które dana osoba powiązała z tym profilem.
z góry dziękuję!
Zaktualizowano dla kodu RanSack
Odpowiedzi:
4 dla odpowiedzi № 1Jeśli czytam twój kod poprawnie, chcesz:
:profile_mentor_areas_mentor_area_cont
0 dla odpowiedzi nr 2
Myślę, że to:
:profiles_mentorings_mentor_area_mentor_area_contains
Zasadniczo, musisz myśleć o tymtabele, do których musisz dotrzeć kolejno po kolei, aby dostać się do pola wyszukiwania. Myślę, że w twoim przypadku widok przewodników musi przejść przez: profiles -> mentoring -> mentor_area, aby wyszukać obszar mentora? Twoja mentor_area ma tylko profile: poprzez model mentoringu. Nie możesz przejść bezpośrednio do mentora, nie przechodząc najpierw przez model mentoringu.
Również,
<%= f.label :guides_profiles_mentor_areas_mentor_area_contains %>
wygląda naprawdę kłopotliwie w widoku. Możesz to po prostu powiedzieć:
<%= f.label "Mentor Area" %>
Mam nadzieję, że to działa.