関連付けを動的に構築するにはどうすればよいですか? 例えば:
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')