Rails console can't connect to database - ruby-on-rails

In last days i update my OS X to Maverics. Today when i try to create new project like this:
rails new abc
there were many problems but i install xcode and now it's work. Right now i open rails console like this:
rails console
and then whatever i write i only see:
Loading development environment (Rails 4.0.1)
1.9.3p448 :001 > Link
=> Link(no database connection)
What is wrong? Mysql is running, database exist. When i do rake db:migrate everything works fine.

The console probably does have a database connection but is reporting that it doesn't.
To see if that's true, make a query in the console.
Link.count
That fixed the false positive warning for me and a colleague.

In the past, referencing an ActiveRecord model immediately after loading the console would return its column information. Rails now only returns this information if it has been explicitly coerced to connect to the database by a method like connection, first, all, count, etc.
You can restore the previous behavior by adding this to your config/application.rb
console do
ActiveRecord::Base.connection
end

According to the issues #12804 on rails/rails.
The message just tells you that AR has not yet connected to the database and therefore does not know the column information. I think it's desired not to connect to the database unless we need to fetch data.
For now you could use Model.connection to establish a connection.

Try using reload! on the console and ensure that you have records in the specified model if not create records for the relations etc..
I had the same problem on ubuntu. I used reload! in rails console and added records in the database.

I'm having the same issue with Rails 4.0.1. It's occurring on the Linux server I'm deploying to as well as my Mavericks development machine.
The server works, specs work, but the console doesn't have a database connection.
Reverting to Rails 4.0.0 fixes the issue with the console.
I haven't found any other mention of this issue. There's probably an issue with the changes for 4.0.1 and the Postgres adapter, maybe? (Are you using Postgres?)

Related

rails c not using the same database as rails db

I just realized that when I run rails c I'm getting a different database than when I run rails db.
I initially though the console wasn't connecting to the database at all because it kept returning empty sets, for instance when running User.all, despite having seed data. I then inserted data into the database via the console and did a query from the SQLite console. The new data did not appear even after reloading the console.
My confusion is further exacerbated by the fact that I only have the db/development.sqlite3 file and rails r "puts Rails.env" returns Development.
So where might I have gone wrong here? Why is one using the development database and the other seemingly using something else? Could rake db:seed be pushing to Test perhaps? Is there a way to check the SQLite console for the environment?
Turns out Spring was causing the problem. I had a previous app with the same name and it was loading that when I opened the rails console despite being in the newer app directory.
I've disabled Spring with the DISABLE_SPRING variable and now things are working as expected.

Firebird database with rails app

When i execute rake db:schema:dump i get in schema.rb:
Could not dump table "xxx" because of following NoMethodError
undefined method `limit' for "INTEGER":String
This is problem with app because on new database i got this same error.
Im using flamerobin and i need to use it for creating db.
Im using https://github.com/rowland/activerecord-fb-adapter to connect to my db.
You're not the only one having this issue apparently: https://github.com/rowland/activerecord-fb-adapter/issues/67. Seems to be a gem issue.
Ok i just reinstall my ruby do everything from the begining (i need to move fbclient.dll from firebird install folder to Disk:ruby/bin) and now everything works great :)

activerecord-sqlserver-adapter seems to have broken devise

I have been working on a rails app for about 2 weeks that has worked fine every time I run it on my local machine. However, today I connected the app to sqlserver (instead of the default sqlite db) with the activerecord-sqlserver-adapter gem.
I was able to successfully connect to the db, but now I receive a undefined method 'email' for <User> on my DeviseSession#new page.
Does anyone know why changing db adapters would cause devise to break?
Because you did not migrate the database after switching the adapter, so the attribute does not exist.
The issue was that I set the table names within my models and forgot their schema prefix.

Rails 4.2.1 fails to establish a connection

I am using ruby 2.2.2
Loading development environment (Rails 4.2.1)
irb(main):001:0> User
=> User (call 'User.connection' to establish a connection)
irb(main):002:0>
The application seems to be working properly. I did insert
<%= console %>
and I was able to do debug in the browser.
I do not have a .irb-rails file (I thought I may have some funky things there).
That was yesterday. Since then, I've upgrade rails to 4.2.3 made a new application
rails new blog
rails g scaffold blog title
rake db:migrate
rails c
irb(main)> Blog
Blog (call 'Blog.connection' to establish a connection)
Can someone tell me what is going on?
Please
The output is telling you exactly what is going on. Rails does not establish a connection to the database until it is needed.
This behavior has been part of Rails since the first releases of 4.x. Here's a GitHub issue talking about it in relation to 4.0.1
If you do what it tells you, and execute Blog.connection, it will connect to the database and you will see the column names in the output.
If this process is annoying to you, you can use the official API for seeing columns names instead: Blog.column_names.
The connection hasn't been established yet because it was not needed.
Try User.find(1) instead, and see if the connection is successful
This make no sense when just call your model try to fetch some data like "Blog.all" this makes connection and fetch all data from blog.
This is an expected behaviour when You have'nt migrated your db.
Just dorake db:migrate or check schema file is generated or not and make connection atleast once before by asking any data.
Something fishy is going on in your environment. Here is my session that works:
zepho-mac-pro:blog zepho$ rails c
Loading development environment (Rails 4.2.3)
2.2.2 :001 > Article
=> Article (call 'Article.connection' to establish a connection)
2.2.2 :002 > Article.count
(0.1ms) SELECT COUNT(*) FROM "articles"
=> 0
When you enter the rails console. It should display Rails 4.2.3 as you see in my example. Your session has Rails 4.2.1. Make sure that you use the right RVM gemset and Ruby version.

Why can't I save any models to PostgreSQL database in Rails?

All of a sudden, I cannot save any models in my PostgreSQL database in Rails. Retrieving data works fine.
m = Model.find(1)
m.save
=> false
When I switched to another database, it is fine. There is no entry in the Rails log.
What could cause this behavior? Is the database corrupt? Is the disk dying? I tried rebooting but that did not help.
How can you check the integrity of the database (I have pgAdmin III installed)? Do I need to do some maintenance? How to figure out what the problem is?
Working in Ubuntu 11.04, Rails 3.0.7 and PostgreSQL 8.3.
Have you added any validations since that object was created? Try this:
> m = Model.find(1)
> m.valid?
> m.errors.full_messages
If something was wrong with your PostgreSQL database you'd probably be seeing exceptions and read errors.
Try a
m.save!
and it should give you more information.
Is the database corrupt? Is the disk dying?
If it is, showing three lines of Ruby code isn't going to help. If you want to know whether your database is working, look at the database. If PgAdmin works, then the db is fine. You might want to turn on query logging in the db then, to see what query (if anything) Rails is sending.

Resources