How to override a column in Rails model?
You can override the col_a
method. Use the read_attribute
method to read the value in database. Something like this:
def col_a
if self.read_attribute(:col_a).to_s.end_with?('0')
0
else
self.read_attribute(:col_a)
end
end
Overriding existing model in Rails 4
The keyword is "NoMethodError Exception: undefined method 'find_or_create_by' for ModelB:Module".
You have a module ModelB
somewhere and, due to load order and/or constant lookup rules, it shadows your model.
Overriding or aliasing name of column in legacy database using Rails/ActiveRecord
I'm just kind of spitballing here, but you might try something like this:
class Legacy < ActiveRecord::Base
#... all the other stuff
#give yourself a way to access the DB version of object_id
def oid
attributes[:object_id]
end
def oid=(val)
attributes[:object_id]=val
end
#restore ruby's default #object_id implementation
def object_id
super
end
end
How can I overwrite a getter method in an ActiveRecord model?
The Rails Style Guide recommends using self[:attr]
over read_attribute(:attr)
.
You can use it like this:
def name
name_trans || self[:name]
end
Related Topics
Write CSV in Ruby 1.9 and CSV::Writer
Rails 3:How to Generate Models for Existing Database Tables
How to Cache a Calculated Column in Rails
Active Admin - Refresh Second Drop Down Based on First Drop Down, Ruby on Rails
How to Randomly Iterate Through a Large Range
Run Ruby Script in Elevated Mode
In Ruby, How to Check If Method "Foo=()" Is Defined
Apply Method to Each Elements in Array/Enumerable
Rubygems Do Not Install on Os X Lion
Is There a Simple Way to Get Image Dimensions in Ruby
How to Format a Number 1000 as "1 000"
Rails3 Devise Undefined Method 'Confirmation_Url'
Why Alias_Method Fails in Rails Model
Ruby Gem Not Found Although It Is Installed
What Does :: (Double Colon) Mean in Ruby