@users.sort! { |a,b| a.name.downcase <=> b.name.downcase } Since strings are comparable, this will sort user objects alphabetically by name. To sort on login_count; @users.sort! { |a,b| a.login_count <=> b.login_count }
Archive for the ‘Ruby on Rails’ Category
Ruby: Sorting an array of objects by an attribute
Posted in Ruby on Rails, tagged ruby, Ruby on Rails on September 28, 2011 | Leave a Comment »
Ruby – Usage of SELF in modules and libraries
Posted in Ruby on Rails, tagged ruby, Ruby programming on September 25, 2011 | Leave a Comment »
Following example gives the best undertanding of it. Also refer; http://www.arthurneil.com/ruby-on-rails/difference-include-extend-ruby module Foo def a puts “a: I am a #{self.class.name}” end def Foo.b puts “b: I am a #{self.class.name}” end def self.c puts “c: I am a #{self.class.name}” end end class Bar include Foo def try_it a Foo.b # Bar.b undefined Foo.c # Bar.c [...]
Difference between include and extend in Ruby
Posted in Ruby on Rails, tagged ruby, Ruby programming on September 25, 2011 | 1 Comment »
include : mixes in specified module methods as instance methods in the target class extend : mixes in specified module methods as class methods in the target class module ReusableModule def module_method puts “Module Method: Hi there!” end end class ClassThatIncludes include ReusableModule end class ClassThatExtends extend ReusableModule end puts “Include” ClassThatIncludes.new.module_method # “Module Method: [...]
Nested classes in Ruby
Posted in Ruby on Rails, tagged ruby, Ruby programming on September 25, 2011 | Leave a Comment »
We can create classes inside another classes in Ruby. In Ruby, classes defined are stored as constants with same name as class. That means any classes (say B,C) defined inside another class (A) would act like constants of that outer class ( as A::B, A::C). class A def test_method “I AM INSIDE CLASS A” end [...]
Ruby on Rails – Multiple database connections
Posted in Ruby on Rails, tagged Ruby on Rails on September 24, 2011 | Leave a Comment »
Define the custom_databse details in database.yml. For example; custom_database: adapter: mysql username: root password: database: custom_development All we need to do is to tell which models to use which connection. (E.g) class Results < ActiveRecord::Base establish_connection :custom_database end
Installing Ruby on Rails, Thin, Nginx, PHP, SVN, Nokogiri and Rmagick/Imagemagick on Ubuntu – Step by step
Posted in installations, Ruby on Rails, tagged Ruby on Rails on September 24, 2011 | Leave a Comment »
Package versions may not be the latest one. These are the steps which I followed to set up my environment. You may want to download the latest source from web and then build it instead of using the download links mentioned here. sudo apt-get install build-essential sudo apt-get install openssl libssl-dev libmysqlclient-dev sudo apt-get install [...]
has_and_belongs_to_many vs has_many through – Difference between them
Posted in Ruby on Rails, tagged Ruby on Rails on September 24, 2011 | Leave a Comment »
Basic rule is; If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship. You should use has_many :through if you need validations, callbacks, or extra attributes on the join model. For example; Stories can belong to many categories. Categories can have many stories. For [...]
Rails new vs build
Posted in Ruby on Rails, tagged Ruby on Rails on September 23, 2011 | Leave a Comment »
new creates a blank new instance, filling attributes with the passed params. It is only used on a Model class: @post = Post.new(attributes) build is only used on association proxies, not on classes, and it does the same as new, but it also sets the foreign key to the id of the parent element: @comment [...]
[Rails] ActiveRecord::AssociationTypeMismatch Object expected got String error – Solution
Posted in Ruby on Rails, Weird Errors, tagged bug fixes, Ruby on Rails on September 23, 2011 | Leave a Comment »
If you are using select (or similar helpers such as collection_select, select_tag) to set a belongs_to association you must pass the name of the foreign key (in the example below blog_id), not the name of association itself. If you specify blog instead of blog_id Active Record will raise an error along the lines of ActiveRecord::AssociationTypeMismatch: [...]
[Rails] Undefined method `select_tag’ for ActionView::Helpers::FormBuilder on f.select_tag
Posted in Ruby on Rails, Weird Errors, tagged bug fixes, Ruby on Rails on September 23, 2011 | Leave a Comment »
Try; f.select not f.select_tag That should work.