Forum: Ruby on Rails make rails connect with database

Posted by Mas Bejo (masbejo)
on 2012-12-13 03:54
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?
Posted by Jim ruther Nill (jimboker)
on 2012-12-13 03:58
(Received via mailing list)
On Thu, Dec 13, 2012 at 10:54 AM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-13 04:05
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"?
Posted by Mas Bejo (masbejo)
on 2012-12-13 04:22
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.
Posted by Jim ruther Nill (jimboker)
on 2012-12-13 05:03
(Received via mailing list)
On Thu, Dec 13, 2012 at 11:22 AM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-13 10:38
i can't understand this command
"rails g scaffold <singular_table_name> --migration false"

can you describe it more?
Posted by Jim ruther Nill (jimboker)
on 2012-12-13 11:08
(Received via mailing list)
On Thu, Dec 13, 2012 at 5:38 PM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-13 16:22
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.
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 01:40
(Received via mailing list)
On Thu, Dec 13, 2012 at 11:22 PM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 02:33
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..
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 02:42
(Received via mailing list)
On Fri, Dec 14, 2012 at 9:33 AM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 02:58
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:in `create'

can you help me??
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 03:08
(Received via mailing list)
On Fri, Dec 14, 2012 at 9:58 AM, Mas Bejo <lists@ruby-forum.com> 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
> rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 03:14
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
-----------------------------------------
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 03:18
(Received via mailing list)
On Fri, Dec 14, 2012 at 10:14 AM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 03:24
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"
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 03:38
(Received via mailing list)
On Fri, Dec 14, 2012 at 10:24 AM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 04:41
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??
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 05:42
(Received via mailing list)
On Fri, Dec 14, 2012 at 11:41 AM, Mas Bejo <lists@ruby-forum.com> 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.
>
>
>


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 07:10
i didn't understand your instruction. what must i run in console? did 
you mean "rails server"? or  what else?
Posted by Jim ruther Nill (jimboker)
on 2012-12-14 07:35
(Received via mailing list)
On Fri, Dec 14, 2012 at 2:10 PM, Mas Bejo <lists@ruby-forum.com> wrote:

> i didn't understand your instruction. what must i run in console? did
> you mean "rails server"? or  what else?
>

i mean rails console.
http://guides.rubyonrails.org/command_line.html#ra...
you really need to go through a tutorial first and learn the basics of
rails.  I suggest
looking at the rails guide for starters.


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


--
Posted by Mas Bejo (masbejo)
on 2012-12-14 11:16
owh, i see.. i forgot that command. i have been try that command in
other app (but when i create database via rails), but i forgot to try
this command in this app.


so, this the step, i try to run this:

"rails console"

and then i run this command:

"Anggotum.all"

it can show all of my record in table.

but why it didn't show at the website, i just see button (show, edit, 
and destroy) but it has leads to the primary key in table member.
Posted by Jim ruther Nill (jimboker)
on 2012-12-15 04:03
(Received via mailing list)
On Fri, Dec 14, 2012 at 6:16 PM, Mas Bejo <lists@ruby-forum.com> wrote:

>
> "Anggotum.all"
>
> it can show all of my record in table.
>

congrats! you're able to connect to the database.  if you have
other errors, please open a new thread about it :)


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


--
Posted by Mas Bejo (masbejo)
on 2012-12-15 05:16
thanks for your help. i know what the problem. i forgot to add syntax to 
show it, because usually it make automaticly. thanks for your help.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.