Recently, I've been interested in learning Ruby on Rails, but at the beginning of my quest I wasn't sure where to begin, there are a lot of documentation in the Web that I could use but I wanted some structured tutorial that guide me through my first experiences with Rails.
Fortunately, the company where I work bought the book Ruby on Rails Up and Running by Bruce A. Tate and Curt Hibbs, so I took it and started reading it and what I found was exactly what I was looking for, a beginners book.
I give this book 4 starts because I think it's really good for someone who is just starting to learn about Rails. It explains the basics of the language and the authors use the same example along each chapter so you can learn all the concepts in the book in a very practical way. The example is a Photo Slideshow application, a very common example actually because it allows you to develop some business logic along with interesting views handled by Ajax and some Javascript effects.
With this book, you can learn the basics of Active Record and Active Record Relationships, Scaffolding, Views, Migrations, Ajax and a bit of Testing.
It is not too long, so you won't feel intimidated by it. It is a good place to start, trust me!
I recommend it!
Technical blog to share some useful information regarding technologies like Java, Grails, JavaScript, etc.
Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts
Wednesday, July 25, 2007
How to handle Time fields in MySQL and Ruby on Rails
One of these days while I was working in a small application created in Ruby on Rails I came across a field in the database with data type Time. The interesting thing was that Rails does not support Time fields, it supports only Date or Datetime, so what to do?
Since I had to handle Time fields not dates, I had to do some tricks to make this work. I'm going to share with you, in a high level, what I did.
First, in the RHTML page I handled the time (hour and minutes) using my own fields, maybe HTML selects with the possible time values.
Next, I created some Javascript function which would post the time as a String or numeric value to the controller, then in the controller you convert the value to a String notation with the format "HH:MM", for example "08:30" or "14:25".
Then, you create a Ruby Time object using your String time as follows:
timeObj = Time.parse("8:15")
This will create a Time object with the current system date and the given hour and minutes you set.
Finally, when you store your Time object in database, MySQL will truncate the date and store only the time value which is exactly what you wanted to do in the first place.
I hope this helps!!
Since I had to handle Time fields not dates, I had to do some tricks to make this work. I'm going to share with you, in a high level, what I did.
First, in the RHTML page I handled the time (hour and minutes) using my own fields, maybe HTML selects with the possible time values.
Next, I created some Javascript function which would post the time as a String or numeric value to the controller, then in the controller you convert the value to a String notation with the format "HH:MM", for example "08:30" or "14:25".
Then, you create a Ruby Time object using your String time as follows:
timeObj = Time.parse("8:15")
This will create a Time object with the current system date and the given hour and minutes you set.
Finally, when you store your Time object in database, MySQL will truncate the date and store only the time value which is exactly what you wanted to do in the first place.
I hope this helps!!
Tuesday, June 19, 2007
Ruby on Rails - Some tips
I've been trying to learn Ruby on Rails, it is an interesting framework that makes developers life a lot easier.
I found some tips or notes that I think I can share with you so they might be helpful when you are doing some programming in Rails yourself.
Hope this helps!!
More coming later...
I found some tips or notes that I think I can share with you so they might be helpful when you are doing some programming in Rails yourself.
Hope this helps!!
- Name your tables in plural, the class would be generated in singular.
- When migrating a schema_info table is created to keep track of the schema version.
- The id column is an int and it's generated automatically, also it uses auto_increment so you don't have to worry to set this value.
- Foreign keys should be named using the name of the class plus "_id", for example product_id.
- Every ActiveRecord class has a transaction method to handle transactions.
- The relationship "belongs_to" corresponds to the "many" side of the many_to_one relationship.
- When you use acts_as_tree, the name convention is parent_id, it would recognize it by itself. This would create a parent and children variables, where children is an array.
- There is a layout file for each controller. You can create just one that can be used by every controller, setting the property layout in the application base controller or directly in every controller.
- In the routes file you can specify the default action to be executed when entering the site. When doing this remember to delete the index.html under the view directory root.
More coming later...
Subscribe to:
Posts (Atom)