lets have person has_many toys. have toy has_many colors. i'm trying in person#show method filter toys contain range of colors.
class person < activerecord::base attr_accessible :name has_many :toys end class toy < activerecord::base attr_accessible :size belongs_to :person has_many :colors end class color < activerecord::base attr_accessible :color belongs_to :toy end
then in personcontroller want filter toys range of colors.
class personcontroller < applicationcontroller def show @person = person.find(params[:id]) # want filter toy colors might red or blue or purple or etc... # when in view @person.toys know contain filtered colors @person.toys.categories end end
and or suggestions appreciated. still actively learning rails.
if want go db approach like:
if params[:toy_colors].nil? @toys = @person.toys else colors = params[:toy_colors].split(',') # note. should check colors array # contains expected colors avoid sql injection. @person.toys.joins(:colors).where('colors in ?', colors) end
where colors passed in param eg.
http://localhost:3000/person/1?toy_colors=red,green
Comments
Post a Comment