Feeds:
Posts
Comments

Archive for the ‘Ruby on Rails’ Category

@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 }

Read Full Post »

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 [...]

Read Full Post »

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: [...]

Read Full Post »

Nested classes in Ruby

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 [...]

Read Full Post »

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

Read Full Post »

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 [...]

Read Full Post »

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 [...]

Read Full Post »

Rails new vs build

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 [...]

Read Full Post »

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: [...]

Read Full Post »

Try; f.select not f.select_tag That should work.

Read Full Post »

« Newer Posts - Older Posts »