Question about inherit models and view

I have some models like this:

class A < ActiveRecord; end
class B < A; end

Now I want to use an action ‘list’ to list out both As and Bs, and I
hope every item shows depending on it model. Like this:

A1: name=xx, age=yy
B1: name=mm, age=uu, sex=male
A2: name=aa, age=bb

I think this is just a question on polymorphism, right?
Thanks.

If you modeled the database as single table inheritance (STI) then
this is not that hard. You do A.find(:all) to get the objects and
some will be Bs rather than As.

In your view you can test for whether they are A or B to present the
right information. Alternatively you can ask them for some piece of
information that you use to form the name of a partial that will be
specific to that type such as _A_row and _B_row with render :partial
=> “#{obj.class.name}_row” or something like that.

Michael

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs