Skip to content

Instantly share code, notes, and snippets.

@shruti28
Last active December 19, 2015 21:29
Show Gist options
  • Save shruti28/6020372 to your computer and use it in GitHub Desktop.
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
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
@ldnunes
Copy link

ldnunes commented Jul 17, 2013

If your Project model is a subclass of the Party model then it has nothing to do with the Person model. You must choose if you want to populate the 'parties' table or the 'persons' table with the new Project object data. You can't do both, unless you duplicate your save logic somewhere in your code.

Also, keep in mind that this will also cause data duplication in your database.

@shruti28
Copy link
Author

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.

@ldnunes
Copy link

ldnunes commented Jul 17, 2013

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