Question

So there are 2 relevant branches. notifications and shipping_options.

I checked notifications out of master, then made 2 significant database changes:

  • Created GeneralNotice model
  • Modified existing Notice model, by adding a boolean dismissed

Then shipping_options was also checked out of master at the same state as notifications, and no database changes were made, only minor code changes.

I ran rake db:migrate the first time switching from notifications to shipping_options and the same vice versa.

At the moment, when I am in notifications, and run rails c I get the following:

2.0.0-p451 :001 > Notice
 => Notice(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime, kind: string, general_notice_id: integer, shop_id: integer, dismissed: boolean) 

2.0.0-p451 :002 > GeneralNotice
 => GeneralNotice(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime) 
2.0.0-p451 :003 > 
  • Notice has dismissed (as expected)
  • GeneralNotice exists (as expected)

Then i switch to shipping_options and I hope you have been following because here comes the confusing part...

2.0.0-p451 :001 > Notice
 => Notice(id: integer, title: string, description: text, created_at: datetime, updated_at: datetime, kind: string, general_notice_id: integer, shop_id: integer, dismissed: boolean) 

2.0.0-p451 :002 > GeneralNotice
NameError: uninitialized constant GeneralNotice
    from (irb):2
    from /Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
2.0.0-p451 :003 > 
  • GeneralNotice doesn't exist, so apparently git is being very nice and helpful by changing the database according to my branch
  • Notice still has a boolean dismissed ... Whaaaat???? Is git being nice or not?? HELP!!!!!!
Was it helpful?

Solution

  1. Since you've run the migration in the first branch (notifications), the column was added to the database table, hence it'll show if you inspect. inspect method works with the database table directly, reflecting the columns (attributes).

  2. GeneralNotice: if you check your database, it would contain the table (general_notices) but since the file general_notice.rb in which GeneralNotice is defined is not present in this branch, it throws uninitialized constant error.

Have a look at this and this for some ideas to put your db under version control.

OTHER TIPS

Whenever you switch branch, and branches might contain set of different migrations it is wise to run rake db:reset which reloads database schema from schema.rb file.

In order to be sure your schema.rb is up-to-date in all branches it is good idea to run rake db:migrate:reset whenever you merge branches with new migrations. This rake task runs all your migrations from the very beginning and writes resulting database scheme to a file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top