Does Rails 3 have find_by association magic? -
specifically, let's assume have 2 sensible models:
tiedyecentipede
,has_many :legs
leg
, has:color
attribute.
being tiedyecentipede
, no 2 legs ever same color. in fact, particular leg's color unique among all of legs of of our tiedyecentipedes
.
based on uniqueness, want find particular centipede
particular color of leg -- let's :deep_sky_blue
.
i like:
critter = leg.find_by_color(:deep_sky_blue).tie_dye_centipede
however, there find_by_*
method on tiedyecentipede
class use well?
no magic:
tiedyecentipede.joins(:legs).where(:legs => {:color => 'deep_sky_blue'}).first
some magic:
def self.find_by_leg_color(color) tiedyecentipede.joins(:legs).where(:legs => {:color => color}).first end
Comments
Post a Comment