-1

I have the following scenario

class XYZ < ActiveRecord::Base
has_many :abcs
end

class ABC < ActiveRecord::Base
belongs_to :xyz
end

class A < ABC
end

class B < ABC
end

class C < ABC
end

The model ABC doesn't have any controller, or view. Data related to ABC will be inserted from the XYZ views and controllers. The user sets a type value for ABC which might be either A, B or C.

And as per the type the corresponding STI subclass must be instantiated and the data must be saved appropriately. But here the subclasses are not getting instantiated, the data is getting saved perfectly. But I am doing it the wrong way as I have written the code of the subclasses into the parent STI class because that code was not getting called in the subclass.

Please give suggesstions and provide some solutions or tutorials.

Thanx in advance.

Rohit
  • 5,631
  • 4
  • 31
  • 59
  • I can't extract what you want, but probably accepts_nested_attributes_for does what you want? – Reactormonk Sep 15 '10 at 07:29
  • I hope this would help you understand better. Consider a CMS which might have Page, NewsItem, BlogItem etc. They could each descend from a common class which in turn inherits from ActiveRecord. The table for each would be the same (title, body, tags, published_at) but each model might have different associations, or different statuses, or a different workflow so each has custom code in their own class. Yet they all share a common table and parent class. It also allow me to use the parent class to do a cross class search and have the resulting Array of records automatically type cast. – Rohit Sep 15 '10 at 07:41
  • I have added the custom code in the child classes but am not able to find a way to instantiate the child class in order to run the code within it. By using the accepts_nested_attributes_for mechanism you accept the attributes of the parent class but you are not able to segregate the child classes. And that is my problem. Hope you have a clearer idea of my problem now. :) – Rohit Sep 15 '10 at 07:43
  • Are you saying that: `ABC.find(some_id)` which has `type` = `X`, returns an instance of `ABC`, when in fact it should return an instance of `X`? – Swanand Sep 15 '10 at 08:27
  • Nope @Swanand. Its not that way. – Rohit Sep 15 '10 at 08:34

1 Answers1

0

Make sure the ABC model has a type attribute, and that it is being set correctly to either "A", "B", or "C" when you save instances of these classes.

ABC.find and friends should then return instances of the respective class.

When creating new objects, you will of course need some code in your controller to call new on the correct subclass depending on some input parameter.

Lars Haugseth
  • 14,721
  • 2
  • 45
  • 49
  • I do have a type column where the type is being set. I am just not getting how do I run the custom code which is present inside the child STI classes. Please show the way to execute that part of code which is present in the child STI classes. – Rohit Sep 15 '10 at 08:32
  • 1
    @Rohit: What kind of code? Class methods, instance methods, or code within the class definition? How and from where do you call that code? You will have to provide some examples. – Lars Haugseth Sep 15 '10 at 08:50
  • Those are instance methods which are present in the child STI classes. – Rohit Sep 15 '10 at 09:46