Search code examples
ruby-on-railsrails-migrations

Do I need a model to insert through IRB?


I have a model for Product and associated tables products & departments in the database.

I don't have a model for Department.

In IRB, I can do this successfully:

p = Product.new

But when I do this:

d = Department.new

It throws,

NameError: uninitialized constant Department

Is that happening because the Rails model for Department isn't there?

If you already have the table, how do you create the model (do I have to generate and run rake db:migrate)?


Solution

  • yes error is due to not having a Department class or model. You can just create a department.rb and inherited from active record in your app/models directory with your favourite editor. for example with vim you can do

    vim app/models/department.rb
    

    edit the above file to have following

    class Department < ActiveRecord::Base
    end
    

    and then reload the irb session with reload!