Comparing fields of 2 table using Arel

373 views Asked by At

Let's say that I have 2 tables joining, table A and table B.

I want to compare 2 fields of these tables

table_A.project(Arel.star).join(table_B).where(table_A[:field_1].eq(table_B[:field_1])) 
1

There are 1 answers

5
Max Williams On BEST ANSWER

To do a join and then compare between fields in different tables, it's best to use the format tablename.columname. So,

table_A.project(Arel.star).join(table_B).where("table_a.field1 = table_b.field2")    

You can use the standard comparison operators with these eg <=, <, >, >= && <> (<> means "not equal to" in sql)

EDIT: note that i have no idea what is produced by "table_A.project(Arel.star)", this is quite mysterious, so for all i know this might cause an error.