dynamically get rails association
What about just doing this?
customer = o.send(relationship)
You can use try()
which will allow you manage any undefined method
errors if the relationship doesn't exist.
relationship = 'customer'
foo = 'foo'
customer = o.try(relationship)
# > [
# [0] #<Customer...
customer = o.try(foo)
# > nil
vs send()
:
customer = o.send(relationship)
# > [
# [0] #<Customer...
customer = o.send(foo)
# > NoMethodError: undefined method `foo' for #<Order...
For those who fears using send
:
o.association(relationship).scope