samedi 17 octobre 2015

Rails query on different models in a multi-level association chain

I have a multi-level model structure, and I am trying to create a query where I can search for either an attribute on the main parent model, or the last model in the associatioon change. My structure looks like the following:

class Parent
    attr_accessible :pname
    has_one :child
end

class Child
    has_many :infos
end

class Info
    has_one :setting
end

class Setting
    has_many :subsettings
end

class Subsetting
    attr_accessible :sname
end

What I am trying to do is create a where query, where i can pull all parents where either the "pname = X" or "sname = X". However, I am unsure of how to create the associations that deep: Is there a way I can do it cleanly using active record, or is it better to create a mysql query directly?

Aucun commentaire:

Enregistrer un commentaire