Thursday, October 25, 2012

Dynamic conditions to rails associations

by sandipransing 0 comments
We all know that rails models associations gets defined while class definitions are loaded and once defined can't be changed. But still you can make use of block parameter to conditions to have dynamic query conditions inside associations.

Below line explains how to define dynamic associations -

has_one :code_sequence, :class_name => 'Sequence', :conditions => 'kind = "#{self.kind}"'

Please make a note that below code won't be working -
has_one :code_sequence, :class_name => 'Sequence', :conditions => proc { |c| ['kind = ?', c.kind] }
Read More…

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter