Last active
December 19, 2015 21:29
-
-
Save shruti28/6020372 to your computer and use it in GitHub Desktop.
can someone explain how the attributes of subclass be saved after it extends some superclass...The type in super class is getting saved bt no attribute is getting saved for subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Party < ActiveRecord::Base | |
attr_accessible :dateCreated, :lastUpdate, :tempForUpdate, :type | |
end | |
>>>>>>>>>> | |
class Project < Party | |
attr_accessible :country, :department, :email, :firstName, :lastName, :location, :mobilePhone, :title | |
end | |
>>>>>>>>>>>> | |
class ProjectsController < ApplicationController | |
def save | |
@project = Project.new(params[:project]) | |
@project.save | |
end | |
end | |
>>>> | |
when i am trying to save project instance,in Party type=Project is set but nothing is entered in Project table.plz help |
When you subclass a model which has a table with a 'type' string column Rails assumes that you want to use the single table inheritance feature. Which means that all database operations for this model will occur in the inherited table; in your case it's the 'parties' table, even if the model is named differently. Rails then uses the type column with the model class name to differentiate between models.
If you want to ignore the single table inheritance for this case and force the Project model to use the projects table use the table_name method in the Project class, like this:
class Project < Party
self.table_name = "projects"
end
Read more about single table inheritance here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class Party < ActiveRecord::Base
attr_accessible :dateCreated, :lastUpdate, :tempForUpdate, :type
end