A very useful rails utility that I totally love in Rails 2.2
Have you ever tried to log in to a remote server’s mysql console ? most remote server’s mysql server especially the ones that come with most VPS hosting will probably be in a different cluster with different ports and hosts. Its a pain to type in that long command just to login to the mysql console. with Rails 2.2 there’s a new utility “dbconsole”. you just login thru ssh go to the base directory of your rails app then type in ’script/dbconsole’ and as simple as that your in. no long arguments , no mistyping of passwords, this should’ve been in the script folder a long time ago. It would’ve saved me a lot of time copying and pasting.
Rails Caching Note : expiring cache when running your app with multiple slices
Note to self :
If you’re using fragment caching and you’re application is deployed to a hosting provider with multiple slices. don’t forget to define the location of the cache and store your cache in a location that all slices have access to. by default rails stores the cache in /tmp/cache , normally I would point it to RAILS_ROOT + tmp/cache
Here’s how to explicitly define the location of your cache:
Just need to define this in your environment.rb or create a file in the initializers directory.
ActionController::Base.fragment_cache_store = :file_store, "/path/to/cache/directory"
Some links about caching that I find very useful:
http://www.railsenvy.com/2007/2/28/rails-caching-tutorial
http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2
Hackathon 2008 Part 1
We had our first ever hackathon over at Exist. Here are the details of the event.
The Mechanics:
- Each team is given a question.
- Each team will create a working web application prototype in 1 day ( 12 hours ), based on the the solution to the question
- With team mates that we will be working with for the first time.
- Every team will start from scratch.
- You can use any tool available.
- The application will be made with rails.
The team I’m in got the question : “I smoke a pack a day. How many years before I die?”. My Teammates are marjun , and rachel. marjun and I work at the same office but rachel works at the cebu office.
We all got our questions 3 days before the event so we had time to research about the question and come up with an idea. I stumbled across this website here. And obviously it answered the question. We were lucky to have been given such an easy question. but The moment I saw that site I have been asking myself questions as to how can our application be different, I guess I was just so obsessed on being different. And thats what I did until the day of the hackathon arrived.
On the day of the hackathon, The first thing we did was find a means of communication. Which gave us our first problem. Our first option was to use yahoo messenger. Since its what we normally use in the office with regular work. but I was using adium, marjun was using pidgin and rachel I think she was using the original yahoo client. So we tried to make a chat room . Unfortunately I’m not sure if this is a bug but I can’t get their invite. so that didn’t work. The next option is to use old school irc. It went fine for a while. but The Cebu office had problems with their connection , it became intermittent and rachel missed a couple of conversations because of that. So we finally chose to use campfire. Went ok from there on, because even if rachel gets disconnected she can still see past conversations and its web based so even if we are using different operating systems, its not an issue.
While undergoing communication problems , we went ahead and tried to discuss how we were going to go about it. Here were our agendas:
1. Which Rails version are we going to use?
2. Which authentication plugin are we going to use?
3. How are we going to build the layout? ( we are three backend developers no ui person )
4. What features are we going to put in?
5. Which plugins/gems are we going to install in order to support the features we decide on?
6. How are we going to split up the tasks?
And thats about it for the first part.
Using datetime_select together with a virtual field
I came across this problem just today, it cost me about 30 minutes to 1 hour just figuring out what I did wrong not to mention the time I spent trying to look for the solution. Luckily I stumbled upon this ticket. here Apparently this problem has already been reported and a patch has already been submitted over a year ago. But it hasn’t been pushed to rails core yet. Anyways to summarize the situation, heres how one can stumble into this problem:
Lets say you have a view which uses the date helper datetime_select like so:
<% form_for(:ticket, :url => tickets_path) do |f| %> <%= f.text_field :title -%> <%= f.text_field :description -%> <%= f.datetime_select :date_this_ticket_might_get_resolved -%> <% end %>
And here’s a sample controller:
class TicketsController < ApplicationController def create @ticket = Ticket.new(params[:ticket]) @ticket.save end end
And lastly the model:
class Ticket < ActiveRecord::Base
def date_this_ticket_might_get_resolved=(val)
self.date_of_submission = Time.mktime( val[:year], val[:month], val[:day],
val[:hour], val[:minute]) + 1.year
end
def date_this_ticket_might_get_resolved
self.date_of_submission + 1.year # add another year just to be sure
end
end
You will get something like this error:
NoMethodError (You have a nil object when you didn't expect it!The error occurred while evaluating nil.klass): /vendor/rails/activerecord/lib/active_record/base.rb:2645:in `execute_callstack_for_multiparameter_attributes' /vendor/rails/activerecord/lib/active_record/base.rb:2644:in `each' /vendor/rails/activerecord/lib/active_record/base.rb:2644:in `execute_callstack_for_multiparameter_attributes' /vendor/rails/activerecord/lib/active_record/base.rb:2630:in `assign_multiparameter_attributes' /vendor/rails/activerecord/lib/active_record/base.rb:2375:in `attributes='
I was using Rails 2.1 with Ruby 1.8.6 , The patch can be found on this link, Just follow the instructions its just a one liner change on ActiveRecord::Base.
Why I chose to start a blog in wordpress.com …
In reference to my previous post , My main reason is that I’m L-A-Z-Y. If I really want this whole blogging thing to work out for me I should set maintenance work at a minimum and this service seems alright for me. A few minutes of setup time , chose my template from a list of pre-made ones ( I’m not good with designs ) then tried out the blogging bundle in my favorite text editor Textmate. So far it feels right.
Trying to blog again.
Blogging hasn’t caught up to me unlike other people. Primarily because I’m lazy and I don’t have much to say. But here I am trying to start another blog. The reason? nothing really I just believe that the shortest route to something you want to get done is to just face it head on. and why I would want it done? Because I wanna see for real how this works out. My past attempts weren’t serious attempts just fiddled with wordpress for a few days then submitted a couple of non-sensical posts. It wasn’t really trying so I wasn’t able to truly assess if this is going to work out for me or not. I’ll probably be blogging about my work ( ruby on rails ), my pastime ( computer gaming ) and/or myself.