Archive for 'Rails'
Search comma separated values db column in Rails
I wanted to search for a value in DB and the column data is comma separated values say …
foo,bar,hey,hay
Now I wanted to find a record based on the value …say …hay
MySQL’s FIND_IN_SET comes into play and it works as follows
1 2 3 | <% Model.find(:first, :conditions => ["FIND_IN_SET(?,search_column)", search_string]) %> |
Hope this helps!
Run Controller Spec
If you just wanted to run only the controller specs, then you have to use the following command
1 | rake spec:controllers |
Select Random Records
I wanted for some reason to have random records from a model without any condition, just random records and here’s how you have to do it.
1 | <% User.find(:all, :order => 'RAND()', :limit => 3) %> |
Hope this helps somebody!
Cheers!!!
Rails Default Layout
As we all know, for any controller, by default rails looks the controller name .rthml files and if it doesn’t exists then it looks application.rhtml.
Since my site is very simple, I don’t have many layouts and just one layout and so i stored the layout file in app/views/layouts/application.html.erb and i removed all other layout files so that by default it will read application.html.erb.
Hope this helps somebody!
Pads
Load Fixtures into the Database
Posted on February 12, 2009, under Rails, Shell Scripts.
I was looking to load the fixtures into my database and it is very simple. Please look at the following code
1 | rake spec:db:fixtures:load |
Hope this helps somebody.
Cheers!
Pads
Migartion in Different Envirnoments
Wanna run db:migrate command in other environments rather than development, then it is how so:
Test Environment
1 | rake db:migrate RAILS_ENV=test |
Production Environment
1 | rake db:migrate RAILS_ENV=production |
Obviously, these commands has to be executed from the root path of the application.
See ya
Hacking a nil object / Workaround
I was looking for a way to workaround a nil object. For instance if @project.name is nil, I wanted to display as “N/A” and otherwise the project name (@project.name)
Normal Way
1 2 3 4 5 | <% unless @project.nil? %> <%= @project.name %> <% else %> <%= "N/A" %> <% end %> |
Another Way
1 | <%= @project.name rescue nil || "N/A" %> |
Just in a one single line!, Neat ah
Hope this helps!
Reset button in Rails
I was looking for a way to have a reset button in the rails way, instead of the direct HTML tag.
1 | <%= submit_tag "Start over", :name => ‘reset’, :type => ‘reset’, :id => ‘task_reset’ %> |
Rmagick on Ubuntu
Posted on January 21, 2009, under Rails, Shell Scripts, Ubuntu.
Hey, Rmagick is a big pain installing it. I was really lazy and tired to install it. Previously in windows version, you need to do lot of configurations for it and it was really time consuming one and also hectic.
But to my surprise installing it in Ubuntu (8.10) was really really a breeze and was so cool and here’s the 2 gracious command to install Rmagick in Ubuntu.
1 2 3 | sudo apt-get install libmagick9-dev ruby1.8-dev sudo gem install rmagick |
Using a partial form with form_for
I just wanted to use partial form for CRUD using Rails 2.2.2 and when i had created a form i got the following error:
undefined local variable or method `f’ for #
Old Code
1 2 3 4 | <% form_for(@project) do |f| %> <%= f.error_messages %> <%= render :partial => 'form' %> <% end %> |
New Code
1 2 3 4 | <% form_for(@project) do |f| %> <%= f.error_messages %> <%= render :partial => 'form', :locals => { :f => f } %> <% end %> |

