如何动态建立关联?例如:
current_customer.company.association(:tenders).order('created_at DESC')
如何动态建立关联?例如:
current_customer.company.association(:tenders).order('created_at DESC')
我猜您想将关联传递给其他方法。您正在寻找的是 send(:method_name, *args)
所以它会是
passed_in_association = :tenders
if( [:tenders,:orders,:users].include?(passed_in_association) ) #for security probably better to add it to a before filter.
current_customer.company.send(passed_in_association).order('created_at DESC')
end
我不确定您到底要做什么,但是send
如果您想使用变量来访问关联,则可以使用(毕竟这只是另一种方法):
current_customer.company.send(:tenders).order('created_at DESC')