STI problem

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

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 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

this is my model
   class Person < ActiveRecord::Base
  end
class parent < Person
end
class Student < person
end
class principal<person
end

i have table called people.
iam adding all the details of parent and student and principal in the
same table.

my form is
     <% form_for :student do %>
             <td><%= text_field :first_name, :label => "Student" %>
              <td><%= text_field :last_name %></td>

         <% fields_for :parent %>
                 <tr>
                  <td><%= text_field :first_name, :label => "Parents/
Guardian" %></td>
                  <td><%= text_field :last_name %></td>
         </tr>
         <tr>
             <td><%= text_field :email, :label => "Email" %></td>
         </tr>
         <tr>
            <td><%= text_field :phone, :label => "Phone" %></td>
         </tr>
         <% end %>
    <% end %>

In the New action i have provided like

     def new
     @student = Student.new
     @parent = Parent.new

  end

my fields in people table are
     t.column :type, :string
      t.column :first_name, :string
      t.column :last_name, :string
    t.column :email, :string
      t.column :phone, :string
      t.column :address, :text

how can we add the student details(first_name, last_name) and the
parent details(first_name, last_name, email, phone) at the same time
with in the same table called people.

Hi Cool,
This post didn't make it onto ruby-forum.com but it is on google.

this is my model
class Person < ActiveRecord::Base
end
class parent < Person
end
class Student < person
end
class principal<person
end

Obviously the above 'person' and 'principal' are capitalized.

i have table called people.
iam adding all the details of parent and student and principal in the
same table.

yes

my form is
<% form_for :student do %>

Not much point doing this if you don't include the parameter. Also,
where is your form posting to?
      <% form_for :student , :url => {:action => 'update'} do |s| %>

         &lt;td&gt;&lt;%= text\_field  :first\_name,  :label =&gt; &quot;Student&quot; %&gt;
          &lt;td&gt;&lt;%= text\_field  :last\_name %&gt;&lt;/td&gt;

Then you can say:
     <td><%= s.text_field :first_name, :label => "Student" %>
Or
     <td><%= text_field :student , :first_name, :label => "Student"
%>
which is the same thing.

     &lt;% fields\_for :parent %&gt;

You don't need "fields_for" - I don't think you're using it below.

             &lt;tr&gt;
              &lt;td&gt;&lt;%= text\_field  :first\_name,  :label =&gt; &quot;Parents/

Guardian" %></td>

So instead, it would be something like:
      <td><%= text_field :parent , :first_name, :label => "Parents/
Guardian" %></td>

And similarly for the rest of these:

              &lt;td&gt;&lt;%= text\_field  :last\_name %&gt;&lt;/td&gt;
           &lt;/tr&gt;
           &lt;tr&gt;
               &lt;td&gt;&lt;%= text\_field :email, :label =&gt; &quot;Email&quot; %&gt;&lt;/td&gt;
           &lt;/tr&gt;
           &lt;tr&gt;
              &lt;td&gt;&lt;%= text\_field  :phone,  :label =&gt; &quot;Phone&quot; %&gt;&lt;/td&gt;
           &lt;/tr&gt;
     &lt;% end %&gt;
&lt;% end %&gt;

See my previous post if you want a dropdown selection box; ie <%=
select ... %>.

In the New action i have provided like

 def new
 @student = Student\.new
 @parent = Parent\.new

end

ok; you could probably even omit creating these.

my fields in people table are
t.column :type, :string
t.column :first_name, :string
t.column :last_name, :string
t.column :email, :string
t.column :phone, :string
t.column :address, :text

ok

how can we add the student details(first_name, last_name) and the
parent details(first_name, last_name, email, phone) at the same time
with in the same table called people.

So:
     <td><%= text_field :parent , :first_name, :label => "Parents/
Guardian" %></td>
will create something like
     <input type="text" name="parent[first_name]" ... />
The important thing is the name attribute.
It will be converted to
     params[:parent][:first_name]
etc

Similarly for principal:
     <td><%= text_field :principal , :first_name %></td>

So in your update action of your controller, you then say:
  @parent=Parent.new(params[:parent])
and similarly for @student and @principal.

Regards,
Daniel

Hi Cool,
This post didn't make it onto ruby-forum.com but it is on google.

> this is my model
> class Person < ActiveRecord::Base
> end
> class parent < Person
> end
> class Student < person
> end
> class principal<person
> end

Obviously the above 'person' and 'principal' are capitalized.

> i have table called people.
> iam adding all the details of parent and student and principal in the
> same table.

yes

> my form is
> <% form_for :student do %>

Not much point doing this if you don't include the parameter. Also,
where is your form posting to?
<% form_for :student , :url => {:action => 'update'} do |s| %>

Sorry. form_for is handy; whether you're passing the parameter in or
not.
It will generate an authenticity token as well as just being more
convenient.
Read up on api.rubyonrails.com. See also form_tag.

u didnt tell me how can we add in the create action at the same time
for people table
  can u send me the code how can we add or an idea of that?

For the controller and the html form, nothing is stopping you from
editing and creating multiple Person objects (Student, Parent, or
Principal) within the one action. Get familiar with rails form
helpers as mentioned previously.

Here's a tip: think in terms of the 'name' attribute in your form:
If it's
  <input name="student[first_name]" value="fred" type="text" ... />
then you'll get
  params[:student][:first_name] => 'fred'
in your controller (when you submit).

Similarly for
  <input name="parent[first_name]" ... />
you'll get
  params[:parent][:first_name]

I've also mentioned how to generate a dropdown instead of a text field
if you want to assign an existing parent to a student.

Then when this form is submitted to your controller you would have
something like:

def update
    @student=Student.new(params[:student])
    # If you are assigning parent to student.
    @student.parent_id = params[:parent][:id] ## if you have dropdown
    @student.save!
    ...
    ## If you are creating the parent alongside the student..
    @parent=Parent.new(params[:parent])
    @parent.save!
    ...
  end

Find some tutorials, read the main pages at api.rubyonrails.com and
buy the rails book http://www.pragprog.com/titles/rails2/agile-web-development-with-rails
. If you're having trouble with rails basics, maybe skip STI for a
bit and just use separate classes.

Daniel