Displaying 4 results from an estimated 4 matches for "attribute_present".
2006 Feb 06
7
Delaying initialization of associations until first access
Guys,
Say I have the following:
-----
class Person < ActiveRecord::Base
has_one :house
end
class House < ActiveRecord::Base
belongs_to :person
attr_accessor :color
end
-----
Then I have the following code:
-----
john = Person.new
john.house.color = "Blue"
john.save
-----
What I would like to have happen, is that on first call to john.house, if
house hadn''t been
2006 Feb 13
0
Including a Module within a model
If I have a module with a callback in it:
module Sanitize
module PhoneNumber
def before_validation
self.phone.gsub!(/\D/, '''') if attribute_present?(''phone'')
end
end
module MobileNumber
def before_validation
self.mobile.gsub!(/\D/, '''') if attribute_present?(''mobile'')
end
end
end
and then I include that within my model:
class Contact < ActiveRecord::Base...
2006 Mar 13
1
adding custom cache field
...39;"
)
end
end
end
class AssociationCollection
def total
count = if has_cached_total?
@owner.send(:read_attribute,cached_total_attribute_name)
end
end
def has_cached_total?
@owner.attribute_present?(cached_total_attribute_name)
end
def cached_total_attribute_name
"#{@reflection.name}_total"
end
end
end
end
module ActiveRecord #:nodoc:
class Base
class << self # Class methods
def modify_value(total_name, id, value)
update_a...
2011 Mar 30
15
Should AR set default values obtained from the schema?
First, if my understanding of what is happening is in error then
please forgive me.
I am given to understand that when a new AR model object is
initialized then AR obtains from the database, and I suppose caches
for further use, those columns that have defaults assigned and the
default value for each.
Consequently, when the save method is called on said model instance
then all of these columns