views:

35

answers:

2
module ActiveRecord
  module Mixin

    alias old_id id

    def id
      old_id.to_i
    end

    def hello
      "hellooooooooooooo"
    end

  end
end


ActiveRecord::Base.send :include, ActiveRecord::Mixin

I make is because:

id column in oracle is number type,not number(10), @user.id return 123.0,not 123,so I would like to do it by extend ar.

But my way above does not work for me,it still show number with dot zero,123.0.

How to make id auto invove id.to_i???

A: 

Try

def new_id
  return self.to_i
end

Then

@user.new_id
Salil
nonononon,I Just want to invore id,not a new method
qichunren
A: 

You want alias_method_chain (docs):

def id_with_oracle
  id_without_oracle.to_i
end
alias_method_chain :id, :oracle

But this may not work. As the id function is dynamically generated from the method_missing magic of ActiveRecord.

Tony Fontenot
Tony Fontenot,thank you,I have tried this,it does not work.
qichunren