Reputation: 3516
FYI this project uses Mongo/Mongoid
Lets say I have models A, B, C, and D. A has_many Bs has_many Cs has_many Ds.
each has an attribute 'name' which is unique in it's association tree, but not necessarily in the whole model class.
If I had a hash like this:
{a: "name1", b: "name2", c: "name3"}
or
{a: "name2", b: "name2", c: "name3", d: "name4"}
How can I find the last model in the hash? Essentially (although it would need to be dynamic to hash size):
@item = A.where(name: "name1").first
.Bs.where(name: "name2").first
.Cs.where(name: "name3").first
Upvotes: 0
Views: 51
Reputation: 1098
You can do with,
@item = A.where(name: "name1").first
@item = @item ? @item.Bs.where(name: "name2").first : Bs.where(name: "name2").first
@item = @item ? @item.Cs.where(name: "name3").first : Cs.where(name: "name3").first
@item = @item ? @item.Ds.where(name: "name4").first : Ds.where(name: "name4").first
Upvotes: 0