Jump to content

The ultimate community for Ruby on Rails developers.


Photo

need help with my database for a simple guestbook


  • Please log in to reply
8 replies to this topic

#1 bigbootykilla

bigbootykilla

    Passenger

  • Members
  • 4 posts

Posted 17 February 2014 - 02:45 AM

Hey guys,

 

I am on the way to learn Ruby in my university. Last week , I had to do a homework to create a simple guestbook with Ruby 1.9.3 and Sinatra 1.4.4. Now I cannot help myself anymore. I have a working guestbook, but I must migrate my guestbook to a sqlite3 database. Until now, the guestbook entries are written in a textfile. The code is very simple, but now I dont know how to use sqlite3 and write the necessary sql statements. I am kinda lost. :(

 

Is there anyone who can show or explain me how I can use my guestbook with sqlite3 instead of that textfile? I would highly appreciate it :)

 

I attached my already written ruby files.

 

greeez Booty

Attached Files



#2 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 54 posts
  • LocationAhmedabad

Posted 17 February 2014 - 04:53 AM

Hey buddy, i am here to help you please ask and explain what you need first we will do it step by step.

 

firstly i have a question that, Have  you completely created your database and application in required ruby and rails versions ?

 

I wants to know status of your application.


Fun with Ruby on Rails


#3 bigbootykilla

bigbootykilla

    Passenger

  • Members
  • 4 posts

Posted 17 February 2014 - 10:59 AM

Thanks buddy! That makes me happy.

 

firstly i have a question that, Have  you completely created your database and application in required ruby and rails versions ?

 

No, we are using Ruby 1.9.3 and Sinatra 1.4.4. Our professor likes Sinatra that much that we wont use Rails much he said. He also would like to teach us to make Ruby with Sinatra and MongoDB, but for now, we are using SQLite3.

 

What I have to do is , writing SQL Statements in a Ruby file, compile it that there is a database.db file.

 

In my already exisiting guestbook, there is a name and a text and a date which is written in that posts.txt file in my attached folder. I want this name, text and date written in a database (sqlite3) instead of a textfile.

 

 

I wants to know status of your application.

 

The status of my appliation is the one I attached. There are only a few files. The next task will be doing all this with a restful api. That means I have to update/delete/edit the guestbook entries for the users. Login is not yet required!



#4 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 54 posts
  • LocationAhmedabad

Posted 17 February 2014 - 01:24 PM

Ok so lets start with this.

 

Create an Application with the following command where you wants (in directry)

rails new gusetbook

Firstly define a database name inside your application in config/database.yml file like below.

development:
adapter: sqlite3
database: your database name
pool: 5
timeout: 5000

Now create models with attributes as you have mentioned in above post

 

Create a migration for user like this

rails g model user email:string password:string

Another one create for guestbook

rails g model guest_book name:string description:text user_id:integer 

Now you have to define relation in modes

 

firstly in /app/models/user.rb

has_many: guest_books, :class_name=>"User", :foreign_key => "user_id"

second one in /app/model/guest_book.rb

belongs_to :user

And finally run the command to migrate all in your database

rake db:migrate

Fun with Ruby on Rails


#5 erowlin

erowlin

    Passenger

  • Members
  • 9 posts

Posted 17 February 2014 - 11:05 PM

Bharat Soni, I disagree with your approach. 

 

The relations between objects, specially theirs foreign keys, should not be specified in the model, but in the database instead. 

rails g model user email:string password:string

 is ok. 

rails g model guest_book name:string description:text user_id:integer

is not ok

 

The good one is : 

rails g model guest_book name:string description:text users:references

"References" type create for you the foreign key and all the relations for the migration. You just have to specify in your model the relations now :  

/app/models/user.rb

has_many :guest_books

 

/app/model/guest_book.rb

belongs_to :user

 

No need for more. 

 

 

Please, before asking help, read the documentation ! http://guides.rubyon...ing-a-migration .



#6 bigbootykilla

bigbootykilla

    Passenger

  • Members
  • 4 posts

Posted 18 February 2014 - 02:15 AM

Hi again,

 

thanks for your help, but arent these database commands only for Rails? I wrote that I am using Sinatra. With Rails I found a lot of tutorials, but not with Sinatra.

 

So, I guess these Rails commands are only for Rails, right?



#7 erowlin

erowlin

    Passenger

  • Members
  • 9 posts

Posted 18 February 2014 - 04:34 AM

Oh my bad yes, but you're on "railsforum.com", i'm not sure you can find a lot of help there. 



#8 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 54 posts
  • LocationAhmedabad

Posted 18 February 2014 - 05:19 AM

Thanks erowlin, you teach me new things and it's very useful.


Fun with Ruby on Rails


#9 bigbootykilla

bigbootykilla

    Passenger

  • Members
  • 4 posts

Posted 18 February 2014 - 11:28 AM

oh okay! Well, i didnt find a Sinatra Forum, so I thought I could use this one here!

 

Our professor told us how easy Sinatra is , but without any example code, its kinda hard to do by myself and understand by myself. I dont know.... always problems with those things :D

 

But anyways... thanks for your help






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users