Does Rails 3 have find_by association magic? -


specifically, let's assume have 2 sensible models:

  1. tiedyecentipede, has_many :legs
  2. 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

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -