Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 2.12 KB

README.rdoc

File metadata and controls

77 lines (55 loc) · 2.12 KB

Rails 3 Single Table Inheritance

Steps to setup this application on local:

  • git clone [email protected]:puneetpandey/rails3_singleTableInheritance.git

  • ${PATH_TO_APP}

  • Double check database.yml file and define settings accordingly.

  • rake db:create

  • rake db:migrate

  • rails s

Example explained

We’ve taken an example of mobile here. 4 types of mobile devices that inherits mobile class

class Mobile < ActiveRecord::Base end; class Samsung < Mobile end; class Apple < Mobile end; class Macromax < Mobile end;

Rails 3 Polymorphic associations

Steps to setup the application is going to remain the same.

class User < ActiveRecord::Base has_many :pictures, :as => :imageable end

class Event < ActiveRecord::Base has_many :pictures, :as => :imageable end

class Picture < ActiveRecord::Base belongs_to :imageable, :polymorphic => true end

Debugger

Debugger support is available through the debugger command when you start your Mongrel or WEBrick server with –debugger. This means that you can break out of execution at any point in the code, investigate and change the model, and then, resume execution! You need to install ruby-debug to run the server in debugging mode. With gems, use sudo gem install ruby-debug. Example:

class WeblogController < ActionController::Base
  def index
    @posts = Post.all
    debugger
  end
end

So the controller will accept the action, run the first line, then present you with a IRB prompt in the server window. Here you can do things like:

>> @posts.inspect
=> "[#<Post:0x14a6be8
        @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
     #<Post:0x14a6620
        @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
>> @posts.first.title = "hello from a debugger"
=> "hello from a debugger"

…and even better, you can examine how your runtime objects actually work:

>> f = @posts.first
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
>> f.
Display all 152 possibilities? (y or n)

Finally, when you’re ready to resume execution, you can enter “cont”.