Problem with entering rails environment with ruby script/console command

Hello,

I’m learning ruby from Patrick Lenz’s “Build your Ruby On Rails Applications”. Please help me with the following -

I’m trying to do the following (copy-paste from book)

To enter a Rails console, change to your shovell folder, and enter the command
ruby script/console, as shown below. The >> prompt is ready to accept your
commands:
$ cd shovell
$ ruby script/console
Loading development environment.
>>

Please answer -

  1. Where exactly is ‘shovell’ folder located?
  2. I’m getting "ruby - no such file or directory – script/console (LoadError)

What am I doing wrong?

have you installed the mysql gem?

K,
Note that the instructions indicate you should “change to your shovell folder”
There was an earlier instruction (it has been quite a while since I went through that book - I don’t remember well enough to guide you more specifically), maybe in a previous chapter, to create a folder for your project (shovell).

Hi ParkinT,

Thanks for your response. I did create ‘shovell’ folder at specified location. I guess I haven’t installed rails properly. I’ll check & get back to you.

Thanks

Did you create a ‘shovell’ folder by running rails shovell ?

Because that is what needed to happen.

I’m a dumba$$!

There was some problem in my Rails installation. I reinstalled everything and it worked like magic.

Thanks for your replies, everyone! :slight_smile:

No, you are not.

“Experience is a cruel teacher; The test comes before the lesson”

aha! Thanks :slight_smile:

Now here’s a (‘serious’) error -

I’m using instant rails and everything’s (servers) up and running!

I’ve created the databases as per the instructions in the Patrick Lenz’s book. I’m in the rails console. Right now, I’m following instructions on Page # 101 (from the ebook)

Here’s the error I’m getting while trying to create instance of the class ‘Story’.

>> class Story < ActiveRecord::Base;end
=> nil
>> story = Story.new
ActiveRecord::StatementInvalid: Could not find table 'stories'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/connection_adapters/sqlite3_adapter.rb:29:in `table_structure'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib
/active_support/core_ext/object/misc.rb:28:in `returning'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/connection_adapters/sqlite3_adapter.rb:28:in `table_structure'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/connection_adapters/sqlite_adapter.rb:189:in `columns'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:1080:in `columns'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2363:in `attributes_from_column_definition_without_lock'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/locking/optimistic.rb:55:in `attributes_from_column_definition'
        from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:1922:in `initialize'
        from (irb):6:in `new'
        from (irb):6
>>

Please note that I’ve created the table ‘stories’ as per following instructions and verified it from the phpMyAdmin:

USE shovell_development;
CREATE TABLE `stories` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`link` varchar(255) default NULL,
PRIMARY KEY (`id`)
);

Can anyone explain why am I not able to create an instance of the class Story? :rolleyes:

Are you running SQLLite or MySQL?
It looksl like your database.yml is referencing SQLLite3 (in the ‘development’ section)

Creating the table via PHPMyAdmin is an odd way of doing this in the Rails world (the author probably used PHP before Rails). Usually you just leave database.yml as is (using SQLite) and start creating migrations. You run this code in the console:

script/generate model story name:string link:string

It generates a model Story plus migration which sets up the table. You can apply this migration to your database by running:

rake db:migrate

If you are following along with the book it’s probably better to use their methods, but remember that there are simpler ways to achieve this for your own applications.

@ ParkinT: I’m using MYSQL. I guessed that too.

@Fenrir2 : Could you please explain what does the code do? Looks to me that the code will migrate my current db to mysql (I’m sorry, if this it totally out of context).

Update:

I ran the commands mentioned by Fenrir2. I checked the database.yml and it was using sqlite3.

So, I’d like to know what magic did we do with

script/generate model story name:string link:string

and rake db:migrate

You know what this command does:

script/generate model story

It generates app/models/story.rb, and empty model file. If you add pairs of field:type the generator will produce a migration for your model. A migration is a script that adds/removes/changes tables in the database.

script/generate model story name:string link:string

This command generates app/models/story.rb plus a migration which creates the table stories, with two colums: name, which is a string (varchar) and link (also a string). The id:integer column is generated automatically.

Now you have a migration, but the changes haven’t been applied to the database yet. The migration is just a script that makes the changes. You can run this migration by executing this command:

rake db:migrate

So now the migration script runs, which executes SQL which creates the stories table with id, name and link columns.

You can view the migration file (it’s a Ruby script) in db/migrate/the-migration.rb. You can also write your own migrations. Here’s more information: http://wiki.rubyonrails.org/rails/pages/UnderstandingMigrations

Cool!

Thanks a lot!

The author of the book will use db mibgrations in the next chapters, but I´d like to know why the book´s example doesn´t work., since i´m having the same issues.

Same here. Following the bouncing ball on pages 102-3 with an InstantRails install does not seem to work. Something to do with the SQLite connector?

but why does the following code

story = Story.new

gives this error
“story = Story.new” RoRActiveRecord::StatementInvalid: Could not find table ‘stories’

when there exists a database table stories inside development, test and production databases that I have created according to book specifications.

it’s sort of a stupid error

I think this may be the cause of the problem. It sounds like the default Rails or InstantRails configuration is using SQLite rather than MySQL. Therefore, you are setting up a MySQL database to work with your application, but the application is pointing at a SQLite database.

Migration will work, because that will use the database prescribed in the database.yml. But manual set up will only work if you use the right database.

I expect you need to modify your database.yml to use the MySQL database. Have a look at the book’s source code and compare your file to the one in the source code.

i have tried both configurations Mysql and SqlLite3 but the thing is i keep getting the same error.