Make rails connect with database

i’m newbie in rails. i have problem with how to connect rails with
database. before i can connect it with command
“rake db:create” to create database.
and “rake db:migrate” to migrate schema to database.

but, i want to connect rails with database that i have before, not
create it via rails. can you help me?

On Thu, Dec 13, 2012 at 10:54 AM, Mas B. [email protected] wrote:

i’m newbie in rails. i have problem with how to connect rails with
database. before i can connect it with command
“rake db:create” to create database.
and “rake db:migrate” to migrate schema to database.

but, i want to connect rails with database that i have before, not
create it via rails. can you help me?

just setup your config/database.yml and that should work. however, when
you want to collaborate with someone, you have to depend on schema.rb
rather than migrations to create the database.

For more options, visit https://groups.google.com/groups/opt_out.

i have been setup database.yml, with same name database that i want to
use.
but then what i should do?
Should i to run “rails generate scaffold”?
let say my command
“rails generate scaffold members name:string email:string” to create
schema??
and then run “rake db:migrate”?

i try open schema.rb, and i saw this code

ActiveRecord::Schema.define(:version => 20121201063411) do

create_table “anggota”, :force => true do |t|
t.string “nama”
t.text “alamat”
t.text “no_telp”
t.datetime “waktu_daftar”
t.datetime “created_at”, :null => false
t.datetime “updated_at”, :null => false
end

create_table “bukus”, :force => true do |t|
t.string “isbn”
t.string “judul”
t.string “penulis”
t.string “penerbit”
t.string “kategori”
t.integer “edisi”
t.integer “jumlah”
t.integer “jumlah_dipinjam”
t.datetime “created_at”, :null => false
t.datetime “updated_at”, :null => false
end

create_table “posts”, :force => true do |t|
t.string “name”
t.string “title”
t.text “content”
t.datetime “created_at”, :null => false
t.datetime “updated_at”, :null => false
end

end

it say create_table “name of table”, but the problem is i have been have
that table in my database, and i didn’t want to change that table.

On Thu, Dec 13, 2012 at 11:22 AM, Mas B. [email protected] wrote:

t.datetime "updated_at",   :null => false
t.integer  "jumlah_dipinjam"

end

end

it say create_table “name of table”, but the problem is i have been have
that table in my database, and i didn’t want to change that table.

first run rake db:schema:dump to generate your schema from the current
database.
Then if you want to run a scaffold (I’ve just tested this one, I don’t
actually use
scaffold), use

rails g scaffold <singular_table_name> --migration false

For more options, visit https://groups.google.com/groups/opt_out.

On Thu, Dec 13, 2012 at 5:38 PM, Mas B. [email protected] wrote:

i can’t understand this command
“rails g scaffold <singular_table_name> --migration false”

can you describe it more?

if you have a table called comments in your current database, and you
want
to generate
the model, controller and views for that table, you need to run

rails g scaffold comment

but that will generate a migration file for creating the table on the
db.
since you
already have the tables, you can pass --migration false to skip creating
the migration.

For more options, visit https://groups.google.com/groups/opt_out.

I always see, every table in rails given the name of the plural form.
however I have a table with a singular form. let say my table name
“member” not “members”, could this create problems?
then the rails command writing table name usually begins with a capital
letter and using the singular form. Can I have a table named in the
singular form, and run the command with the same name?

I have tried, but it can not display the contents of my database. there
are things that make me confused, it was formed six lines of command
“show, edit, and destroy”, an amount equal to the number of records in
the table “member”. but does not display the contents of the table
“member” it.

On Thu, Dec 13, 2012 at 11:22 PM, Mas B. [email protected] wrote:

the table “member”. but does not display the contents of the table
“member” it.

try looking for set_table_name syntax to set the table name that you
want
to use

For more options, visit https://groups.google.com/groups/opt_out.

i can’t understand this command
“rails g scaffold <singular_table_name> --migration false”

can you describe it more?

did you mean add this syntax

set_table_name “member”

in app\models\member.rb??

i have been try it, but it didn’t work…

On Fri, Dec 14, 2012 at 9:33 AM, Mas B. [email protected] wrote:

did you mean add this syntax

set_table_name “member”

in app\models\member.rb??

i have been try it, but it didn’t work…

well just saying it didn’t work won’t help you at all. describe what
happens and why
you concluded it didn’t work.

For more options, visit https://groups.google.com/groups/opt_out.

i try to add this syntax

set_table_name “member”

in member.rb at folder app\models\

but same as before it can’t show record in table member, and i try to
click new member, i saw this error

“ActiveRecord::StatementInvalid in MemberController#create”

and this error

app/controllers/member_controller.rb:46:in block in create' app/controllers/member_controller.rb:45:increate’

can you help me??

On Fri, Dec 14, 2012 at 9:58 AM, Mas B. [email protected] wrote:

and this error

app/controllers/anggota_controller.rb:46:in block in create' app/controllers/anggota_controller.rb:45:in create’

can you post your code on anggota_controller?

To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.

On Fri, Dec 14, 2012 at 10:14 AM, Mas B. [email protected] wrote:

end
end
end

  end
    format.html { redirect_to @anggotum, notice: 'Anggotum was

DELETE /anggota/1

end


that the code of anggota_controller.rb

Where do you use the member table? I think you need to read on the MVC
architecture first before you start working on this one. I really can’t
help you
if you have no firsthand knowledge of how the models and controllers are
connected.

For more options, visit https://groups.google.com/groups/opt_out.

class AnggotaController < ApplicationController

GET /anggota

GET /anggota.json

def index
@anggota = Anggotum.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @anggota }
end

end

GET /anggota/1

GET /anggota/1.json

def show
@anggotum = Anggotum.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @anggotum }
end

end

GET /anggota/new

GET /anggota/new.json

def new
@anggotum = Anggotum.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @anggotum }
end

end

GET /anggota/1/edit

def edit
@anggotum = Anggotum.find(params[:id])
end

POST /anggota

POST /anggota.json

def create
@anggotum = Anggotum.new(params[:anggotum])

respond_to do |format|
  if @anggotum.save
    format.html { redirect_to @anggotum, notice: 'Anggotum was 

successfully created.’ }
format.json { render json: @anggotum, status: :created,
location: @anggotum }
else
format.html { render action: “new” }
format.json { render json: @anggotum.errors, status:
:unprocessable_entity }
end
end
end

PUT /anggota/1

PUT /anggota/1.json

def update
@anggotum = Anggotum.find(params[:id])

respond_to do |format|
  if @anggotum.update_attributes(params[:anggotum])
    format.html { redirect_to @anggotum, notice: 'Anggotum was 

successfully updated.’ }
format.json { head :no_content }
else
format.html { render action: “edit” }
format.json { render json: @anggotum.errors, status:
:unprocessable_entity }
end
end
end

DELETE /anggota/1

DELETE /anggota/1.json

def destroy
@anggotum = Anggotum.find(params[:id])
@anggotum.destroy

respond_to do |format|
  format.html { redirect_to anggota_url }
  format.json { head :no_content }
end

end
end


that the code of anggota_controller.rb

no, i didn’t use member table, i use member word just to describe to you
singular and plural form. because i little confuse tell you plural form
from word “anggota”

On Fri, Dec 14, 2012 at 10:24 AM, Mas B. [email protected] wrote:

no, i didn’t use member table, i use member word just to describe to you
singular and plural form. because i little confuse tell you plural form
from word “anggota”

ok. Just make sure that you’re using the table name on the db on the
Anggota
model. If you’re doing that and still gets errors, I don’t know what
your
issue is.
I can’t see any errors on your controller. It seems like it’s the one
generated by
the scaffold generator so there shouldn’t be any errors in there.

Try running the console and see if Anggota.first retrieves the first
record.

For more options, visit https://groups.google.com/groups/opt_out.

  1. Rails new First_app
  2. Change config database.yml
  3. Rake db:schema:dump
  4. Rails g scaffold Anggota –migration false
  5. Change First_app\app\model\anggotum.rb with this syntax

class Anggotum < ActiveRecord::Base
set_table_name “anggota”
end

i have try to make new rails app, and that is the step by step that i
did.
did i do wrong step??

On Fri, Dec 14, 2012 at 11:41 AM, Mas B. [email protected] wrote:


i have try to make new rails app, and that is the step by step that i
did.
did i do wrong step??

everything looks ok. did you try it on the console?

For more options, visit https://groups.google.com/groups/opt_out.

i didn’t understand your instruction. what must i run in console? did
you mean “rails server”? or what else?