Skip to content

Instantly share code, notes, and snippets.

@diatmpravin
Last active December 18, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diatmpravin/5744138 to your computer and use it in GitHub Desktop.
Save diatmpravin/5744138 to your computer and use it in GitHub Desktop.
Add `#any_of` query method to active_record
1.9.3-p392 :060 > Address.all
Address Load (0.4ms) SELECT "addresses".* FROM "addresses"
+----+---------+--------+---------+---------+----------+-------+--------+-------------------------+-------------------------+
| id | user_id | extra | city | country | postcode | state | street | created_at | updated_at |
+----+---------+--------+---------+---------+----------+-------+--------+-------------------------+-------------------------+
| 1 | 1 | ankit | asansol | india | 71475 | w.b. | 100 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
| 2 | 1 | sanyam | asansol | india | 71476 | w.b. | 101 | 2013-06-09 09:22:05 UTC | 2013-06-09 09:22:05 UTC |
| 3 | 2 | abhay | asansol | india | 71476 | w.b. | 102 | 2013-06-09 09:22:05 UTC | 2013-06-09 09:22:05 UTC |
| 4 | 3 | abhay | bettiah | india | 71476 | w.b. | 102 | 2013-06-09 09:22:05 UTC | 2013-06-09 09:22:05 UTC |
+----+---------+--------+---------+---------+----------+-------+--------+-------------------------+-------------------------+
4 rows in set
1.9.3-p392 :061 > User.all
User Load (0.3ms) SELECT "users".* FROM "users"
+----+------------------+--------+--------+------------+-------------------------+-------------------------+
| id | email | fname | lname | phone | created_at | updated_at |
+----+------------------+--------+--------+------------+-------------------------+-------------------------+
| 1 | ankit@gmail.com | ankit | mishra | 9999999999 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
| 2 | abhay@gmail.com | abhay | mishra | 88888888 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
| 3 | sanyam@gmail.com | sanyam | mishra | 7777777777 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
+----+------------------+--------+--------+------------+-------------------------+-------------------------+
3 rows in set
1.9.3-p392 :062 > user = User.any_of({fname: 'ankit'}, {phone: 7777777777})
User Load (0.3ms) SELECT "users".* FROM "users" WHERE (("users"."fname" = 'ankit' OR "users"."phone" = 7777777777))
+----+------------------+--------+--------+------------+-------------------------+-------------------------+
| id | email | fname | lname | phone | created_at | updated_at |
+----+------------------+--------+--------+------------+-------------------------+-------------------------+
| 1 | ankit@gmail.com | ankit | mishra | 9999999999 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
| 3 | sanyam@gmail.com | sanyam | mishra | 7777777777 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
+----+------------------+--------+--------+------------+-------------------------+-------------------------+
2 rows in set
1.9.3-p392 :064 > user.first.addresses.any_of({extra: 'ankit'},{street: 101})
Address Load (0.5ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."user_id" = ? AND (("addresses"."user_id" = 1 AND "addresses"."extra" = 'ankit' OR "addresses"."user_id" = 1 AND "addresses"."street" = 101)) [["user_id", 1]]
+----+---------+--------+---------+---------+----------+-------+--------+-------------------------+-------------------------+
| id | user_id | extra | city | country | postcode | state | street | created_at | updated_at |
+----+---------+--------+---------+---------+----------+-------+--------+-------------------------+-------------------------+
| 1 | 1 | ankit | asansol | india | 71475 | w.b. | 100 | 2013-06-09 09:22:04 UTC | 2013-06-09 09:22:04 UTC |
| 2 | 1 | sanyam | asansol | india | 71476 | w.b. | 101 | 2013-06-09 09:22:05 UTC | 2013-06-09 09:22:05 UTC |
+----+---------+--------+---------+---------+----------+-------+--------+-------------------------+-------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment