ruby on rails - Active record queries - chaining -


i trying convert following query better, using parameters example.

i new active record , rails still learning. null part throwing me off. since needs work on both mysql , sql server.

contacts = contacts.where("key_contact = true , (c_contact null or c_contact = false)") 

i should not key_contact , c_contact in different table called main_contacts. contacts table has_many main_contacts

anyone help?

i'd say:

contacts = contacts.where(key_contact: true).where(c_contact: [nil, false]) 

in console check resulting query appending .explain

should equivalent to:

contacts = contacts.where(key_contact: true).where.not(c_contact: true) 

anyway, should keep consistency in database. booleans, have default value prevent null.


per comment:

contacts = contacts.joins(:main_contacts).where(main_contacts: { key_contact: true }).where.not(main_contacts: { c_contact: true }) 

Comments