Tag Archives: ActiveRecord

ActiveRecord::ReadOnlyRecord while updating object fetched by joins

ActiveRecord find with join options retrieves object as readonly. station = Station.find( :first, :joins => :call, :conditions => [“customer_id = ? and date(insurance_expiry_date) = ?”, customer.id, insurance_expiry_date ] ) Readonly object cannot modified.  station.update_attributes({ :customer_id => 12 }) should raise … Continue reading

Posted in Ruby on Rails | Tagged , | Leave a comment

Manual active record db connection in ruby using mysql adapter

Here is the code to make manual connection with database require ‘active_record’ActiveRecord::Base.establish_connection( :adapter => “mysql”, :host => “localhost”, :username => “root”, :password => “abcd”, :database => “funonrails” ) Load database configurations from yml file dbconfig = YAML::load(File.open(‘database.yml’))ActiveRecord::Base.establish_connection( dbconfig )

Posted in Ruby on Rails | Tagged , | 2 Comments

How to map irregular database tables with rails models

Rails specifies standard conventions while creating models, controllers, migrations ( database tables ). Conventions for creating database tables 1. Table name should be plural 2. id field must be primary_key for table. 3. foreign_key  must be like <model_name>_id i.e. post_id, … Continue reading

Posted in Ruby on Rails | Tagged , , | 2 Comments