mysql - joining tables in active records ruby -


i have 2 tables, table1

id   name   age   occupation  ssn_id  1         18    engineer    12 2    b      19    engineer    67 3    c      14    student     89 4    b      19    engineer    67 

table2

id   ssn_id  limit   hours 1    67      2       20 2    12      2       17 3    89      2       78 

expexted output

ssn_id  limit  count(*)  occupation 67      2      2         engineer 

sql query -

select id, occupation, count(*) total, `limit` table1*table2 table1.ssn_id = table2.ssn_id , occupation = 'engineer' group table1.ssn_id  having count(*) > `limit` 

i want convert query rails active record query. have tried using include can't columns of second table.

if have models table1 , table2, can use approach:

 table1.    joins(:table2).    select("table1.id, table1.occupation, count(*) total, table2.limit").    where(occupation: 'engineer').where("table1.ssn_id = table2.ssn_id").    group(table1.ssn_id).    having("count(*) > limit") 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo