I’m trying to fetch associations of a model dynamically. For ex;
model1 has_many model2s
model1 has_many model3s
normally i can do x.model2s or x.model3s. But what if i dont know which association is wanted. Is there any snippet like : x["model2s"] to call associations dynamically?
Thanks
Çağdaş.
Yes, you can do
x.send("model#{i}s")assuming you have the number saved inior justx.send(foo)if you have the full name as string or symbol saved infoo.Documentation: