読者です 読者をやめる 読者になる 読者になる

コピペコードで快適生活

明日使えるソースを自分のために

RailsのArelの使い方

Rails Ruby

RailsでArelを使うときは、下記サイトをいつも参照させていただいていましたが、自分メモ用として転載します。
メモ || Arelのwhere系メソッド一覧 User.w...

User.where(User.arel_table[:name].eq("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE "users"."name" = 'aa'

User.where(User.arel_table[:name].eq(nil)).to_sql
# => SELECT "users".* FROM "users" WHERE "users"."name" IS NULL

User.where(User.arel_table[:name].eq_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" = 'aa' AND "users"."name" = 'bb'))

User.where(User.arel_table[:name].eq_all(["aa",nil])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" = 'aa' AND "users"."name" IS NULL))

User.where(User.arel_table[:name].eq_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" = 'aa' OR "users"."name" = 'bb'))

User.where(User.arel_table[:name].eq_any(["aa",nil])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" = 'aa' OR "users"."name" IS NULL))

User.where(User.arel_table[:name].not_eq("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" != 'aa')

User.where(User.arel_table[:name].not_eq(nil)).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" IS NOT NULL)

User.where(User.arel_table[:name].not_eq_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" != 'aa' AND "users"."name" != 'bb'))

User.where(User.arel_table[:name].not_eq_all(["aa",nil])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" != 'aa' AND "users"."name" IS NOT NULL))

User.where(User.arel_table[:name].not_eq_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" != 'aa' OR "users"."name" != 'bb'))

User.where(User.arel_table[:name].not_eq_any(["aa",nil])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" != 'aa' OR "users"."name" IS NOT NULL))


User.where(User.arel_table[:name].gt("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" > 'aa')

User.where(User.arel_table[:name].gt_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" > 'aa' AND "users"."name" > 'bb'))

User.where(User.arel_table[:name].gt_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" > 'aa' OR "users"."name" > 'bb'))

User.where(User.arel_table[:name].gteq("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" >= 'aa')

User.where(User.arel_table[:name].gteq_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" >= 'aa' AND "users"."name" >= 'bb'))

User.where(User.arel_table[:name].gteq_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" >= 'aa' OR "users"."name" >= 'bb'))


User.where(User.arel_table[:name].lt("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" < 'aa')

User.where(User.arel_table[:name].lt_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" < 'aa' AND "users"."name" < 'bb'))

User.where(User.arel_table[:name].lt_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" < 'aa' OR "users"."name" < 'bb'))

User.where(User.arel_table[:name].lteq("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" <= 'aa')

User.where(User.arel_table[:name].lteq_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" <= 'aa' AND "users"."name" <= 'bb'))

User.where(User.arel_table[:name].lteq_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" <= 'aa' OR "users"."name" <= 'bb'))


User.where(User.arel_table[:name].in(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE "users"."name" IN ('aa', 'bb')

User.where(User.arel_table[:birthday].in('1980-01-01'.to_date..'1990-12-31'.to_date)).to_sql
# => SELECT "users".* FROM "users" WHERE "users"."birthday" BETWEEN '1980-01-01' AND '1990-12-31'

User.where(User.arel_table[:name].in_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" IN ('aa') AND "users"."name" IN ('bb')))

User.where(User.arel_table[:name].in_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" IN ('aa') OR "users"."name" IN ('bb')))

User.where(User.arel_table[:name].not_in(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" NOT IN ('aa', 'bb'))

User.where(User.arel_table[:name].not_in_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" NOT IN ('aa') AND "users"."name" NOT IN ('bb')))

User.where(User.arel_table[:name].not_in_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" NOT IN ('aa') OR "users"."name" NOT IN ('bb')))


User.where(User.arel_table[:name].matches("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" LIKE 'aa')

User.where(User.arel_table[:name].matches("%aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" LIKE '%aa')

User.where(User.arel_table[:name].matches_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" LIKE 'aa' AND "users"."name" LIKE 'bb'))

User.where(User.arel_table[:name].matches_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" LIKE 'aa' OR "users"."name" LIKE 'bb'))

User.where(User.arel_table[:name].does_not_match("aa")).to_sql
# => SELECT "users".* FROM "users" WHERE ("users"."name" NOT LIKE 'aa')

User.where(User.arel_table[:name].does_not_match_all(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" NOT LIKE 'aa' AND "users"."name" NOT LIKE 'bb'))

User.where(User.arel_table[:name].does_not_match_any(["aa","bb"])).to_sql
# => SELECT "users".* FROM "users" WHERE (("users"."name" NOT LIKE 'aa' OR "users"."name" NOT LIKE 'bb'))