views:

690

answers:

4

Has anyone seen where script/console and script/server load two different databases (though both report using the same)? Here's the first output

$ script/server
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-03-21 15:54:05] INFO  WEBrick 1.3.1
[2010-03-21 15:54:05] INFO  ruby 1.8.7 (2010-01-10) [i386-mingw32]
[2010-03-21 15:54:05] INFO  WEBrick::HTTPServer#start: pid=7148 port=3000

No errors. I then run my standard code for entering a form - no problems.

Checking the Dev Database (.yml at bottom):

mysql> select * from books;
[...]
| 712 | Book | Book Name | 2010-03-21 22:29:22 | 2010-03-21 22:29:22 |
[...]
712 rows in set (0.00 sec)

The code CLEARLY saved it seconds ago

And now here's the output of script/console:

$ script/console
Loading development environment (Rails 2.3.5)
>> Book.all
=> []

Nothing. Further, upon further inspection, it's using the production database, but I can't figure out why. Any thoughts here? All consoles have been closed and reopened.

UPDATE: Requested .yml file (can't see how it'd be helpful (user name and password are all the same for each)) -

development:
  adapter: mysql  
  database: BooksDBdev
  username: <user name>
  password: <long string>
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: BooksDBtest
  username: <user name>
  password: <long string>
  timeout: 5000

production:
  adapter: mysql
  database: BooksDB
  username: <user name>
  password: <long string>
  timeout: 5000

Updated 2: Here's the output when I force the console environment.

$ script/console development
Loading development environment (Rails 2.3.5)
>> Book.all
=> []
>> exit

$ ruby script/console development
Loading development environment (Rails 2.3.5)
>> Book.all
=> []

Just FYI, I don't have RAILS_ENV specified in my environment variables.

A: 

Pluralized model name? Yikes! Do you have another model called Book or something?

Beerlington
Not real names :) ... it's not plural irl.
aronchick
A: 

Are you running these at the same time, in different terminals? If so, you may need to do reload! in your console for the database changes to show up.

Ben
Same machine, different terminals... yes - but I've closed all the terminals many times. Regardless, it's not a model change - as you can see, it's the data access that's messed up (accessing the wrong DB).
aronchick
Ben
Yeah, when I do, the ruby script/console fails! As I said, weirdly, even though it claims it's using development, it seems to be using production. But not the server!
aronchick
A: 

Model have to be singular. This is kind of convention.

Book.all

not

Books.all

fl00r
Is your model called books.rb in app/models/ and is your class Books < ActiveRecord::Base really pluralized?
fl00r
NO. This is just an example. Fine I'll change the language.
aronchick
It's not cool when your examples are broken. Ok, here can be two problems:1. Your missed something in your database settings (user access to read/write etc)2. Your model is not connected to your table in db properly.
fl00r
Unfortunately, these are both not correct. You can see that the model does connect to the database, as per the example, which means the permissions and the models are both correct, they're just connecting to the wrong db.
aronchick
Ok, let's figure out what database is active.In console run this: Book.configurations[RAILS_ENV]['database']
fl00r
And secondly. Run just this: "Book" - to see if Model isn't mapped to DB so it is AR trouble
fl00r
Bizarre!! Book.configurations['development']['database']Returns my _production_ database when run from the console! But when I do the same in my app (ruby script/server - create a dummy page that spits this out), it says my _development_ database.
aronchick
Maybe something wrong with your environment.rb or other environment configuration files. Is your app is fresh one and you or somebody else haven't played with it's configurations?
fl00r
A: 

I have also get the same behaviour. Is this Rails' bug?

jaycode