Forum: Ruby on Rails Question about inherit models and view.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5c4533f23a9d7d67ffe1ea5539b4130b?d=identicon&s=25 Magicloud (Guest)
on 2007-05-27 04:31
(Received via mailing list)
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?
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-05-27 09:27
(Received via mailing list)
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
=> "#{}_row" or something like that.

This topic is locked and can not be replied to.