Veerasundaravel's Ruby on Rails Weblog

July 2, 2014

Rails – Force update ActiveRecord updated_at column

Filed under: Ruby On Rails — Tags: , , , , , , — Veerasundaravel @ 12:11 am

When we usually modify any specific column/field in our model, Active-record will check whether the corresponding value is new or not.

If it is new then only it will create a DB query and update that value with current timestamp in updated_at column, if there is no change in existing value means it will only fire commit query only. There wont be any change in updated_at column.

So if we want modify the timestamp of updated_at field without updating any field means, we can follow any of below two methods:

Method 1:
user.update_attributes(:name => “same old name”, :updated_at => Time.now)

Method 2:
user.touch

Both of above two methods will modify updated_at value with current timestamps even if there is any change in other fields or not.

 

Advertisements

September 17, 2010

ActiveRecord – Tableless Model

If you want to create an ActiveRecord model without any table reference, and want to add validation for that model means, you can go with this example code.

In app/models/base_model.rb

class BaseModel < ActiveRecord::Base
def self.columns
@columns ||= [];
end

def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
sql_type.to_s, null)
end

# Override the save method to prevent exceptions.
def save(validate = true)
validate ? valid? : true
end
end

In app/models/post.rb

class Article < BaseModel
column :title, :string
validates_presence_of :title
end

In script/console

Loading development environment (Rails 2.2.2)
>> article = Article.new
=> #<Article title: nil>
>> article.valid?
=> false
>> article.errors
=> #<ActiveRecord::Errors:0x235b270 @errors={“title”=>[“can’t be blank”]}, @base=#<Article title: nil>>


Reference Links:

http://stackoverflow.com/questions/315850/rails-model-without-database/
http://railscasts.com/episodes/193-tableless-model
http://agilewebdevelopment.com/plugins/activerecord_base_without_table

February 8, 2008

ActiveRecord reload in Migration


Today I faced one problem , Like I want to add one column in existing model user. And want to update the field value in next line itself as follows:

class CreateAdminStructure < ActiveRecord::Migration
def self.up
add_column :users, :admin_flag, :boolean, :default=>false
user=User.find(1)
user.admin_flag=true
user.save!
end

def self.down
remove_column :users, :admin_flag
end
end

So when I’m trying to assign a value to newly added field admin_flag like ‘user.admin_flag=true’, it return some error like unknown column admin_flag.

The problem is newly added admin_flag column in users table will not come to effect within the same migration file on that instance. So to avoid this issue we might reload the table structure.

So when ever you are trying to update/access newly added fields within migration, you have to tell the ActiveRecord about the changes.
For that we have one option like
.reset_column_information

You can use the .reset_column_information to force ActiveRecord to notice the changes that the migration has made to the database.

class CreateAdminStructure < ActiveRecord::Migration
def self.up
add_column :users, :admin_flag, :boolean, :default=>false
User.reset_column_information
user=User.find(1)
user.admin_flag=true
user.save!
end

def self.down
remove_column :users, :admin_flag
end
end


Reference Url:

            http://railsapi.org/activerecord-base-reset_column_information

%d bloggers like this: