Added associations (has_many & belongs_to) to README
commit 646784e84d007848203bf00d99e3f3d757586da1 Author: Holger Frohloff <holger.frohloff@gmail.com> Date: Sat Oct 6 23:11:36 2012 +0200 Added attributes to model descriptions modified: README.md commit 60e62d1e847ebdb948e01f41997a91d768f99fbb Author: Holger Frohloff <holger.frohloff@gmail.com> Date: Sat Oct 6 23:09:29 2012 +0200 Renamed @search object modified: README.md commit c547afaf0aaea3ec07a62298ba933a332bf01755 Author: Holger Frohloff <holger.frohloff@gmail.com> Date: Sat Oct 6 23:06:19 2012 +0200 Added associations to README modified: README.md
This commit is contained in:
parent
953ec1b502
commit
928e63f965
50
README.md
50
README.md
|
@ -103,6 +103,56 @@ Once you've done so, you can make use of the helpers in Ransack::Helpers::FormBu
|
|||
construct much more complex search forms, such as the one on the
|
||||
[demo page](http://ransack-demo.heroku.com).
|
||||
|
||||
### has_many and belongs_to associations
|
||||
|
||||
You can easily use Ransack to search in associated objects.
|
||||
|
||||
Given you have these associations ...
|
||||
|
||||
class Employee < ActiveRecord::Base
|
||||
belongs_to :supervisor
|
||||
|
||||
# has attribute last_name:string
|
||||
end
|
||||
|
||||
class Department < ActiveRecord::Base
|
||||
has_many :supervisors
|
||||
|
||||
# has attribute title:string
|
||||
end
|
||||
|
||||
class Supervisor < ActiveRecord::Base
|
||||
belongs_to :department
|
||||
has_many :employees
|
||||
|
||||
# has attribute last_name:string
|
||||
end
|
||||
|
||||
... and a controller ...
|
||||
|
||||
class SupervisorsController < ApplicationController
|
||||
def index
|
||||
@search = Supervisor.search(params[:q])
|
||||
@supervisors = @search.result(:distinct => true)
|
||||
end
|
||||
end
|
||||
|
||||
... you might set up your form like this ...
|
||||
|
||||
<%= search_form_for @search do |f| %>
|
||||
<%= f.label :last_name_cont %>
|
||||
<%= f.text_field :last_name_cont %>
|
||||
|
||||
<%= f.label :department_title_cont %>
|
||||
<%= f.text_field :department_title_cont %>
|
||||
|
||||
<%= f.label :employees_last_name_cont %>
|
||||
<%= f.text_field :employees_last_name_cont %>
|
||||
|
||||
<%= f.submit "search" %>
|
||||
<% end %>
|
||||
|
||||
|
||||
## Contributions
|
||||
|
||||
To support the project:
|
||||
|
|
Loading…
Reference in New Issue