[newbi] Create a Model

Hello All,

Yesterday i put RoR with Locomotive. I am very happy :p.
So I follow this tuto
(http://developer.apple.com/tools/rubyonrails.html). I used Sqlite.

my database.yml

MySQL (default setup). Versions 4.1 and 5.0 are recommended.

Get the fast C bindings:

gem install mysql

(on OS X: gem install mysql – --include=/usr/local/lib)

And be sure to use new-style password hashing:

http://dev.mysql.com/doc/refman/5.0/en/old-client.html

development:
adapter: sqlite
dbfile: db/expense_developement

Connect on a TCP socket. If omitted, the adapter will connect on

the

domain socket given by socket instead.

#host: localhost
#port: 3306

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: expenses_test
username: root
password:
socket: /tmp/mysql.sock

production:
adapter: mysql
database: expenses_production
username: root
password:
socket: /tmp/mysql.sock

PostgreSQL versions 7.4 - 8.1

Get the C bindings:

gem install postgres

or use the pure-Ruby bindings on Windows:

gem install postgres-pr

postgresql_example:
adapter: postgresql
database: expenses_development
username: expenses
password:

Connect on a TCP socket. Omitted by default since the client uses a

domain socket that doesn’t need configuration.

#host: remote-database
#port: 5432

Schema search path. The server defaults to $user,public

#schema_search_path: myapp,sharedapp,public

Character set encoding. The server defaults to sql_ascii.

#encoding: UTF8

Minimum log levels, in increasing order:

debug5, debug4, debug3, debug2, debug1,

info, notice, warning, error, log, fatal, or panic

The server defaults to notice.

#min_messages: warning

SQLite version 2.x

gem install sqlite-ruby

sqlite_example:
adapter: sqlite
database: db/development.sqlite2

SQLite version 3.x

gem install sqlite3-ruby

sqlite3_example:
adapter: sqlite3
database: db/development.sqlite3

In-memory SQLite 3 database. Useful for tests.

sqlite3_in_memory_example:
adapter: sqlite3
database: “:memory:”

When i tabe this line in Terminal
$ script/generate Model Expense

I show only that
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/expense.rb
create test/unit/expense_test.rb
create test/fixtures/expenses.yml

i didn’t see this commands
create db/migrate
create db/migrate/001_create_expenses.rb

I don’t have this folder and this file

Thanks

Bolo

i think i had a similar problem. if you are using sqlite3 on your
system
then you’ll want to use the sqlite3 adapter, not the sqlite adapter.

give that a shot and see if it does anything. I could be way off base.

Yup, that’s right - if it’s actually sqlite3, then you should use
adapter:sqlite3

So i must change my database.yml
like that
development:
adapter: sqlite3
dbfile: db/expense_developement

2006/3/31, Tom A. [email protected]:

this my new file

development:
adapter: sqlite3
dbfile: db/expense_developement

Connect on a TCP socket. If omitted, the adapter will connect on

the

domain socket given by socket instead.

#host: localhost
#port: 3306

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: sqlite3
database: expenses_test

production:
adapter: sqlite3
database: expenses_production

but always same thing

$ script/generate Model Expense
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/expense.rb
identical test/unit/expense_test.rb
identical test/fixtures/expenses.yml

2006/3/31, Bolo M. [email protected]:

for First point

  1. rails -d sqlite3 /path/to/you/new/railsapp
    u write this command with Locomotive ?

For me
Ordinateur-de-Bolo-Michelin:~/Documents/Creation_Perso/Developement/expenses
bolomichelin$ rails -d sqlite3
/Documents/Creation_Perso/Developement/expenses

invalid option: -d

2006/3/31, Scott M. [email protected]:

sorry to reiterate some points, but i use sqlite3 successfully so…

easy steps for rails 1.1

  1. rails -d sqlite3 /path/to/you/new/railsapp
  2. cd /path/to/you/new/railsapp
  3. rake db:schema:dump
  4. ./script/generate model MyNewModel

step 1 can be ommitted for an existing app - obviously! but it saves you
changing anything in database.yaml except the db name

step 3 may not be exact syntax - old syntax of rake db_schema_dump still
works, which is all i’ve used yet - this step creates the database if it
doesn’t already exist.

step 4 auto creates the migration as of rails 1.1

hth

sorry, i may have confused the issue - is locomotive rails 1.1? i’m on
linux,
not mac osx

i believe the
-d, --database=name Preconfigure for selected database
(options:
mysql/oracle/postgresql/sqlite2/sqlite3)

options are only 1.1 (or i didnt notice them before)

like i said though it just means one less step - you can still just edit
your
database.yaml file (which is essentially all that option does)

if you’re rails 1.0 then build the app, edit the database.yaml and
uncomment
the line

config.active_record.schema_format = :ruby

in environment.rb

before doing the db dump using rake db_schema_dump

you’ll have to create your migrations manually using

./script/generate migration create_my_model_name

or whatever

so

  1. ./script/generate migration create_my_model_name
  2. rake migrate
  3. ./script/generate model MyModelName

hope this helps - sorry for any confusion

Yes But maybe the problem from Locomotive. I don’t know
I use Rail 1.1

in terminal i tape this
Ordinateur-de-Bolo-Michelin:$ d, --database=expenses
-bash: d,: command not found

Ordinateur-de-Bolo-Michelin:$ railsd, --database=expenses
-bash: railsd,: command not found

Ordinateur-de-Bolo-Michelin:$ rails d, --database=expenses
invalid option: --database=expenses

No Working for me :frowning:
I tested the second solution
Ordinateur-de-Bolo-Michelin:$ ./script/generate migration Expense
create db/migrate
create db/migrate/001_expense.rb

Ok After i create my table in 001_expenses
class Expense < ActiveRecord::Migration
def self.up
create_table :account do|table|
table.column :name, :string
table.column :budget, :float
end
end

def self.down
drop_table :accounts
end
end

Then

Ordinateur-de-Bolo-Michelin:$ rake migrate
(in /Users/bolomichelin/Documents/Creation_Perso/Developement/expenses)

and all.
My table was not created :frowning: :frowning:

2006/3/31, Scott M. [email protected]:

AND!
you may want to check out DHH’s screencast on migrations if you haven’t
already done so- http://media.rubyonrails.org/video/migrations.mov

ok first off it’s either

rails -d sqlite3 /path/to/app

OR

rails --database=sqlite3 /path/to/app

running rails --help will tell you all this - but like i said its only a
shortcut

migration looks ok, apart from :float - im not 100% if sqlite3 supports
floats
you better check the docs (sqlite.org) - i dont use them (i would store
cents
in the db rather than dollars for instance, and do the necessary
translation
in the model)

i’m assuming
create_table :account do|table|
should be
create_table :accounts do |table|

here’s one lifted from a working app

class CreateTaggings < ActiveRecord::Migration
def self.up
create_table :taggings do |t|
t.column :tag_id, :integer, :limit => 11, :null => false
t.column :taggable_id, :integer, :limit => 11, :null => false
t.column :taggable_type, :string, :limit => 255, :default => ‘’,
:null
=> false
end
add_index ‘taggings’, [‘tag_id’], :name => ‘fk_taggings_tag’
end

def self.down
drop_table :taggings
end
end

that said, are you sure the database exists after dumping the schema?
file
permissions ok? you dont mention any error messages
only other thing that i can think of is that its something to do with
locomotive, which i cannot help with

good luck

I delete the app and i create a new.
my database.yml


development:
adapter: sqlite3
database: db/expense_development.sqlite3

test:
adapter: sqlite3
database: db/expense_test.sqlite3

production:
development


my table accounts


class Account < ActiveRecord::Migration
def self.up
puts ‘Creating Table: accounts…’
create_table :accounts, :force => true do |t|
t.column :name, :string
t.column :budget, :float
end
end

def self.down
puts ‘Deleteing Table: accounts’
drop_table :accounts
end
end


when i teste rake migrate ,i have this now
$ rake migrate
(in /Users/bolomichelin/Documents/Creation_Perso/Developement/expense)
rake aborted!
parse error on line 9, col 12: `’
:frowning:

2006/3/31, Scott M. [email protected]:

i wouldnt’t be putting the puts … statements in there if i were you -
lose
them

have you tried :integer instead of :float

rails 1.1 use db:migrate and it will give you a verbose output of what
it’s
doing - i’ve given you both syntaxes already

Working !!
production:
adapter: sqlite3
database: db/expense_production.sqlite3
i change my database.yml :stuck_out_tongue:

thanks

2006/3/31, Bolo M. [email protected]:

cool - thought you had that ok anyway, was it the file extension?