From bf4294eae1702e0536ba38aba9358593cf05d588 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 6 Sep 2014 01:30:37 +0200 Subject: [PATCH] README: Add an example of attribute chaining to the Associations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit section, and improve the “Grouping queries by OR instead of AND” section. [skip ci] --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fe54657..b332b19 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ class Supervisor < ActiveRecord::Base belongs_to :department has_many :employees - # has attribute last_name:string + # has attributes first_name:string and last_name:string end ``` @@ -271,8 +271,8 @@ end <%= f.label :department_title_cont %> <%= f.search_field :department_title_cont %> - <%= f.label :employees_last_name_cont %> - <%= f.search_field :employees_last_name_cont %> + <%= f.label :employees_first_name_or_employees_last_name_cont %> + <%= f.search_field :employees_first_name_or_employees_last_name_cont %> <%= f.submit "search" %> <% end %> @@ -409,7 +409,9 @@ Employee.search({ salary_gt: 100_000 }, { auth_object: current_user }) ### Grouping queries by OR instead of AND The default `AND` grouping can be changed to `OR` by adding `m: 'or'` to the -query hash. In the Rails console: +query hash. + +For example, in the Rails console: ```ruby artists = Artist.search(name_cont: 'foo', style_cont: 'bar', m: 'or') @@ -424,7 +426,7 @@ artists.result.to_sql OR \"artists\".\"style\" ILIKE '%bar%'))" ``` -Try it with and without `m: 'or'` in the query hash. Notice that the combinator becomes `or` instead of the default `and`, and the SQL query becomes `WHERE...OR` instead of `WHERE...AND`. +The combinator becomes `or` instead of the default `and`, and the SQL query becomes `WHERE...OR` instead of `WHERE...AND`. This works with associations as well. Imagine an Artist model that has many Memberships, and many Musicians through Memberships: