activerecord - Rails, get record with has_and_belongs_to_many having more than one value -


given these models

modelone     has_and_belongs_to_many :model_twos  modeltwo     has_and_belongs_to_many :model_ones      field_one: string 

how use active record modelones had associated modeltwos field_one equalled "value_1" , "value_2"

something modelone.joins(:model_twos).where(model_twos:{field_one: "value_1" , "value_2"})

this indeed tricky, if want use activerecord (i.e. without falling arel).

i'm not 100% work or produce desirable sql i'd try this:

class modelone < activerecord::base   scope :with_model_two_value, -> (val) { joins(:model_twos).where(model_twos: { field_one: val }) } end  modelone.with_model_two_value('value_1').merge(modelone.with_model_two_value('value_2')) 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -