Setup database on new clone

3 views
Skip to first unread message

sreid

unread,
Jul 20, 2011, 7:00:45 PM7/20/11
to Ruby on Rails: Talk
I've just cloned my rails app onto another PC, and I'm having a
problem getting the database set up.

I've tried rake db:migrate, then db:create and also db:reset, but I
keep getting an SQLException "no such table". It seems this is due to
a line in an initializer which accesses one of my tables.

Why do the db:* tasks run the initializers ? Do I need to move any db
code out of initalizers - if so, where to ?

Kendall Gifford

unread,
Jul 20, 2011, 8:18:59 PM7/20/11
to rubyonra...@googlegroups.com
On Wednesday, July 20, 2011 5:00:45 PM UTC-6, sreid wrote:
I've just cloned my rails app onto another PC, and I'm having a
problem getting the database set up.

I've tried rake db:migrate, then db:create and also db:reset, but I
keep getting an SQLException "no such table". It seems this is due to
a line in an initializer which accesses one of my tables.

Why do the db:* tasks run the initializers?

I'm guessing because it was easier to implement this way. All task implementations can be simply assuming that the full rails environment is available.
 
Do I need to move any db
code out of initalizers

Either that or write said initializers so they can fail gracefully in the event the database (or tables therein) isn't/aren't setup.
 
- if so, where to?

Whatever code path eventually "queries" whatever data/settings/state you set/configure from your database can use memoization:

  def my_settings_from_database
    @my_settings_from_database ||= load_my_settings_from_database
  end

  private
  load_my_settings_from_database
    # your initializer code
  end

Colin Law

unread,
Jul 21, 2011, 4:12:21 AM7/21/11
to rubyonra...@googlegroups.com

To get you out of the hole I imagine if you manually create the db
(empty) then you will probably be ok.

Colin

sreid

unread,
Jul 21, 2011, 11:18:20 AM7/21/11
to Ruby on Rails: Talk
Thanks for the suggestions. I ended up using the method described
here : http://stackoverflow.com/questions/3485059/running-rake-dbmigrate-without-some-initializers

Philip Hallstrom

unread,
Jul 21, 2011, 11:34:15 AM7/21/11
to rubyonra...@googlegroups.com

This may be helpful... in helping get your initializer code to run "later" so to speak...

http://www.bigbinary.com/videos/2-how-rails-boots

If not, it's an interesting video anyway :)

-philip

Reply all
Reply to author
Forward
0 new messages