When I manually enter tables into the .rb file in the migrate folder nothing shows up. I have followed a few different "how to's" but I can't get the tables to show up when I preview my work. I can however get one table to show up if I create it in the parameters when generating a scaffold, but that's one table. I have Rails version 2.3.4 and version 1.2.3 installed on my lame Windows Vista. I have tried using both versions multiple times. I am also trying to follow the Ruby on Rails for Dummies book, but it seems a little outdated. Any tips on getting tables to work? Its supposed to be really simple and take "just minutes" to complete this simple app. Maybe something didn't install right? Maybe a better book?
Just want to make sure you're using all the tools available to you:
To generate a migration file:
./script/generate MigrationFileName
Take a look at the Database Migrations Guide from the official Rails Guides for the correct syntax and options for creating a migration file.
Once you've created your migration, you need to run the following:
rake db:migrate
This will populate the database with the information specified in the migration file.
Related
I’m trying to add a string column to a SQLite 3 database in rails. Use the normal syntax "rails generate migration AddAuthorColumnToPublications author:string". I run the migration, it works without errors.
I change the attributes accessor in the Publications model to include the Author column. I check in the schema, the new column has been included in the schema.
I go to my database and the new column does not appear in the index of the Publications table. I’ve tried including it specifically in the index, but to no avail.
The only thing I can think of here is that because I am using Ubuntu, the syntax may be slightly different (as it has been for certain things).
I have looked in books and on the internet, and the syntax seems correct (can also use underscores as well as camel casing to name the migration), but I can’t find anything specific to Ubuntu on this particular issue.
I would appreciate any and all help on this matter.
Run this command:
rake db:migrate
Migrations are not run automatically. You need to run rake db:migrate in order to run all the migrations and update the database.
Also, review your database.yml file and make sure that you're using the sqlite database that you're opening. The syntax of the command is not different.
I'm having huge problems with my populate.rake file - after importing my existing file from my Rails 2.3.5 installation, Rails 3 wants nothing to do with it. I'm getting tens (if not hundreds) of errors in my console, many of them for simple statements, and some even in the middle of a string of plaintext.
Has the syntax changed for Rails 3 populate or, perhaps do I need to install a gem/plugin for it?
Thanks very much.
db-populate is a Rails plugin which uses a populate.rake file, it's not core Rails functionality so yes, you'd need a gem/plugin for it (http://github.com/ffmike/db-populate).
However, since you've upgraded to Rails 3, it might be a nice time to switch to using the built-in Rails seed functionality - that way it's easy to run rake db:setup which will create the db from database.yml, load the schema, and run seeds.rb or rake db:reset which does the same thing after dropping the db. Keeps you from having to maintain third-party code as well. Just food for thought. :)
I found the problem after carefully reconstructing the populate.rake file from a copy. Some of the sample text I introduced into the file had quotes and these were interfering with the quotes belonging to the string itself.
Once Rails interprets the string as having ended, then it logically starts to return errors within the successive strings, which it is reading as interpretable code.
Once I removed all the quotes from within my strings during that reconstruction, the file started working. Thank you Ryan and JenJenut for your replies!
I'm a designer and brand new to programming.
I have just opened my localhost and can see "Welcome aboard". It has 3 steps to complete - all of which simple to most, are complexing to me.
config/database.yml ok it wants to know my username and password. Where do I get these and where do I post them to to get them to work? I opened my database.yml and it is
SQLite version 3.x
gem install sqlite3-ruby
I hear people talking about git and all this but confused on how to do these 3 steps -
1.
Create your databases and edit config/database.yml
Rails needs to know your login and password.
2.
Use script/generate to create your models and controllers
To see all available options, run it without parameters.
3.
Set up a default route and remove or rename this file
Routes are set up in config/routes.rb.
Is this just too over my head? I wouldn't mind hiring someone to teach get it all going for me because I really want to learn to code Ruby on Rails.
Thanks!
Note - All resolved by deleting and reinstalling. Now I understand what is actually happening.
Since you're using an SQLite database, you don't need to configure any username or password to get going with the database -- the default configuration should work out of the box and creates a new database file automatically for you.
For your other questions, the Getting Started with Rails guide over at guides.rubyonrails.org is an excellent introduction that walks you through all the steps in creating a new Rails application. Start by going through this tutorial step by step -- if you get stuck somewhere along the way you can always come back here and post follow-up questions.
If you don't have any idea what a database is you should just leave database.yml alone and use the provided defaults (use sqlite, does not require a password/login). For getting started with Rails, this should be enough. Just delete the public/index.html file.
I would really recommend you to buy a book (Rails 3) (3rd edition is for Rails 2.3) which guides you through the creation of a Rails app - with a lot of examples. The guides are an alternative, but also might be a little difficult to read with close-to-none preknowledge of programming.
I think you might find Michael Hartl's Ruby on Rails Tutorial helpful. It's available for purchase as a PDF or you can view the online version for free. It covers all the major steps of developing a Rails application, including using Git.
I do my Rails dev from xterm and in vim.
I'm getting sick of running script/generate migration do_whatever, then trawling through db/migrate trying to tab-complete to 20091015235018_do_whatever when there's 5 other migrations with similar timestamps.
What's the best way to add a hook to open the generated migration in vim?
I'd rather not hack into Rails' core in /usr/lib as I work from several systems and can see myself wanting to create numerous such hooks. However, it doesn't really seem plugin worthy.
There are lots of simple ways to get the name of the generated file, but I'm not sure how to cleanly hook it into the generation. What do you think?
A better option with vim is to use rails.vim and type :Rmigration do<TAB>
It ignores the timestamp ...
You can also create and edit a migration in one go by typing :Rgenerate migration ...
I just submitted a patch to Rails to add an --editor option for just this case, which you could backport into whatever version of Rails you're using now. If you don't specify which editor you want it tries to use EDITOR
I want to run two rails websites (homepage and app) on the same database. However, migrations dont work because both websites try to use schema_migrations table at the same time.
Is it possible to override default schema_migrations table name? Any other ideas how to solve this problem?
The schema_migrations table name is kept in ActiveRecord::Migrator.schema_migrations_table_name, which you might me able to override (in environment.rb, initializers, etc.), but I haven't tried this.
On the other hand, if you use unique migration IDs in both application (default in 2.1 onwards, I think), migrations from two applications should work with a single schema_migrations table.
See this screencast for more information on how migrations work in Rails 2.1 and up.
I don't know when this was added but Rails 4 seems to support it now.
From the Rails documentation
config.active_record.schema_migrations_table_name lets you set a string to be used as the name of the schema migrations table.
If you are using a version of Rails where this is not supported then an alternative could be to use table_name_prefix. If you are using this approach, I'd make sure that your version of Rails prefixes schema migrations with table_name_prefix by looking at the source code.