0

如何动态建立关联?例如:

current_customer.company.association(:tenders).order('created_at DESC')
4

2 回答 2

2

我猜您想将关联传递给其他方法。您正在寻找的是 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
于 2012-10-04T02:58:32.460 回答
1

我不确定您到底要做什么,但是send如果您想使用变量来访问关联,则可以使用(毕竟这只是另一种方法):

current_customer.company.send(:tenders).order('created_at DESC')
于 2012-10-04T02:57:11.307 回答