# STI problem

**URL:** <https://www.ruby-forum.com/t/sti-problem/150007>\
**Category:** Rails\
**Created:** [October 2, 2008, 10:28am UTC](https://www.ruby-forum.com/t/sti-problem/150007 "2008-10-02T10:28:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@cool](https://www.ruby-forum.com/u/cool)\
**Post date:** [October 2, 2008, 10:28am UTC](https://www.ruby-forum.com/t/sti-problem/150007/1 "2008-10-02T10:28:32Z")

</div>

I have Person model in that i have used STI like  
class person \< ActiveRecord  
class student \<person  
class parent \< person

for every thing i have table called people.

when i am adding a student i have fields for parent also like  
i want to add a student with firstname and last name and at the same  
time i mean in the same form i wnat to add to which parent he belongs  
to in the same form i have fields like parent name, email, address

can u give idea abt this

---

<div class="post-metadata">

**Author:** ![cool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@cool](https://www.ruby-forum.com/u/cool)\
**Post date:** [October 2, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/sti-problem/150007/2 "2008-10-02T13:19:13Z")

</div>

cool wrote:

> I have Person model in that i have used STI like  
> class person \< ActiveRecord  
> class student \<person  
> class parent \< person
> 
> for every thing i have table called people.
> 
> when i am adding a student i have fields for parent also like  
> i want to add a student with firstname and last name and at the same  
> time i mean in the same form i wnat to add to which parent he belongs  
> to in the same form i have fields like parent name, email, address

This was quite hard to read. Use full stops or bullet points or  
something.

> can u give idea abt this

So, you have something like  
class Person \< ActiveRecord::Base …  
in app/models/person.rb  
And:  
class Parent \< Person …  
class Student \< Person …  
maybe in app/models/parent.rb and student.rb respectively.  
And your ‘people’ table has a column called ‘type’ which is a string  
field.

In your edit form you could do something like:  
\<% form\_for :student , :url =\> {:action =\> ‘update’} do |s| %\>  
\<%= s.text\_field :first\_name %\>  
\<%= s.text\_field :last\_name %\>  
… parent stuff - see below …  
… submit button etc …  
\<% end %\>

Within this form you would have something like:  
\<%= select :selected\_parent , :id ,  
Parent.find(:all).collect{|p|[p.last\_name,p.id]} %\>  
which will generate a dropdown with existing parents last\_names and  
their id’s.

Or if you are entering parents details in alongside the student,  
something like:  
\<%= text\_field :parent, :first\_name %\>  
\<%= text\_field :parent, :last\_name %\>

Read [api.rubyonrails.com](http://api.rubyonrails.com) and go to ActionView::Helpers::FormHelper and  
FormOptions for how to use the form fields and what they generate in  
terms of the params your controller will be receiving.

In your controller, the ‘update’ action would do something like:

def update  
…  
@student=Student.new(params[:student])  
@student.parent\_id = params[:selected\_parent][:id]  
@student.save!

(‘parent\_id’ is a field for storing the id of the parent in your  
‘people’ table.)  
You could save the parent using associations instead but I won’t go into  
it.

If you were collecting parent details using the text fields above, you  
might have:

```
@parent=Parent.new(params[:parent])
@parent.save!

```

Daniel
