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 -
Where exactly is ‘shovell’ folder located?
I’m getting "ruby - no such file or directory – script/console (LoadError)
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).
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.
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:
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.
@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).
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.
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.
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.