0

I am looking to pass an sti base class as a param and then be able to call all the child classes from that query. br/ This is my STI Class & Subclasses.

class LivingThing < ActiveRecord::Base
end

class Animal < LivingThing
end

class Plant < LivingThing
end

class Fungus < LivingThing
end

class Cat < Animal
end

class Dog < Animal
end

I would like to pass a param through a rendered form called "living_thing_type" so I can do this:

<%= render 'form', living_thing_type: "Animal" %>

living_things = living_thing_type.all  

but even when I try this

living_thing_type.constantize.all

I get this error:

undefined method `all' for "Animal":String
NothingToSeeHere
  • 2,253
  • 5
  • 25
  • 57
  • 1
    What did you assign to `living_thing_type`? If `living_thing_type` is indeed 'Animal', it won't throw the `undefined method `all' for "Animal":String` as you are doing `constantize` on the string which will return the `class` of `Animal`. – Dharam Gollapudi Mar 25 '16 at 05:44
  • I did constantize and it worked! I wanted to give you the chance to answer that here as it's not very easy to find documentation for that for me. – NothingToSeeHere Mar 26 '16 at 22:50

0 Answers0