Thursday, July 28, 2011
In Rails 3.1 ActiveRecord::Migration we can define only one method
It`s 'reversible' migrations. You just implement 'up' version and Migration system generate for you methods to create revers migration.
When you run migration 'up' it create a new table, if 'down' - drop dable. But you must be careful using this magick, because some methods can`t be resolved and you get ActiveRecord::IrreversibleMigration
Rails known how to revert this commands:
There for you can create 'change' migration. Try to run it 'up' and 'down', and if you migration raised, you just implement 'down' version.
I think it`s one more beautiful issues that presents to us Rails platform.
Friday, April 29, 2011
Friday, April 22, 2011
You must be very careful when create "before_*" callbacks in Rails.
I found one interesting feature (of course it is not bug :D ) ActiveRecord raise with error RecordNotSaved if you have "before_*" callback that return "false". For example:
You can`t save instance of this class if you don`t change "name" because method "set_short_link" return false (see /activerecord/lib/active_record/base.rb, line 2568)
Right version will be:
Friday, November 19, 2010
As most of the time I work with Ruby and Ruby on Rails and Emacs is my favorite text editor I would like to devote my next post to the Emacs configuration.
Last week I have been customizing Emacs in order to simplify my work. Therefore, i would like to share my work with you.
I started with Emacs theme customization and used color-theme plugin, which can be found on Emacs color-theme plugin homepage or can be taken from Debian repository in emacs-goodies-el package. After the installation you have three options:
- Use one of the build-in themes (M-x color-theme-select);
- Create your own theme;
- Use mine :) (color theme)
For the options 2 and 3 you have to save the theme file into Emacs path directory and add the following line into your ~/.emacs file:
Also, as you have noticed, I set the custom font using set-frame-font (the font was chosen in xfontsel), disabled: toolbar, menubar and scrollbar.
Afterwards, I chose Rinari from a large list of Emacs Rails plugins. Rinari homepage.
The base Rinari directory is placed into Emacs path directory and the following code is added to ~/.emacs
The last hook for ruby-mode deactivates Emacs coding tags, automatically created by Emacs coding-system (that can be very uncomfortable while working with files that have Cyrillic symbols).
Unfortunately, Rinari does not include ruby-electric (helper for automatic brackets closure or any other language construction). To add ruby-electric copy ruby-electric.el to Emacs path directory and configure it into ~/.emacs
Generally, Emacs uses only one major editing mode, for example, ruby-mode in *.rb files. However, if we want to edit files that contain ruby and HTML we will have to either switch the modes every time or we can install MuMaMo (MultiMajorModes). MuMaMo automatically highlights the code and switches the major modes. Most often MuMaMo is used in *.html.erb files to edit ruby and HTML. MuMaMo is based on nXhtml. You can download the latest version of nXhtml (MuMaMo included) here.Unzip the archive and copy it into Emacs path directory. Add to ~/.emacs the following:
As I don`t like colored hunks, I set the variable MuMaMo-chunk-coloring being equal 5. This means that the coloring will turn on only for the fifth level, however, usually we see only three levels maximum.
Interactively Do Things
To speed up file management in you working directory you can use IDO helper (Interactively Do Things), which is a part of Rinari package. If you dislike it you can disable it any time.
Git in Emacs
As I use git a lot I included git-emacs (link) in Emacs as well:
In the last line I bound git-status function to C-x M-g combination.
Autocomplete plugin can be downloaded here. Unpack the plugin, copy to Emacs path and configure it:
As autocomplete has a short completion list. I used Rsense to extend autocomplete functionality. Further on I will tell how to install and configure Rsense in Debian (more detailed information for other OS you can find in Rsense installation guide).
So, having downloaded and unpacked the archive copy it into /opt directory and add local variable RSENSE_HOME that will keep the path to Rsense root directory. Afterwards, check whether bin/rsense can be executed and then create config file from Rsense configuration script.
What comes to the local variable RSENSE_HOME, add to ~/.bashrc the following line using your favorite editor:
Save the file, close the editor and open new console tab to re-read the configuration file.
That is all about Rsense installation.
Autocomplete with Rsense
To configure Emacs for Rsense and automplete + Rsense work:
That`s all folks! :) You can borrow my final version of ~/.emacs file here
Have a nice coding on Ruby on Rails with Emacs!
Thursday, October 28, 2010
`map` returns new array which we join using "," string.
Also, you can put the data into SQL query manually. For example, adding any random integer values to our data:
However, pay attention! Of course, `map` does return the new array, but you, probably, think that the `a << rand (100)` adds the new value to the output array only. You are wrong! The thing is that when you work with `a` inside the `map` iterator, you work with the basic array, as by default, the variables are passed by link in Ruby. Therefore, when the new data is added in the existing array, you will get the following:
To create a new instance you must use the operation like ‘+’
Thus, don’t forget that variables in ruby are passed by link and when you work with ruby iterators you work with the basic data.