Hi you all,
I have a model simulating an ActiveRecord model, according to the tips
stated in [1]. It is simulated because even though I am gonna save the
attributes in a file, not in a DB, I still want the validations made by
ActiveRecord::Base (see code below).
Well, the thing is that I also want the behaviour of the created_on and
updated_on columns, that get modified automatically before saving.
The problem is that I have my own “save” method and it does not work
(remember this is not a real ActiveRecord model). I’ve tried to set the
before_create and before_updated methods but they does not work (see
code below).
So… is there any way to do it automatically?
Thanks.
[1]
http://wiki.rubyonrails.org/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord
, Alternative 2.
CODE:
class PageMetadata < 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
column :abstract, :string
column :title, :string
column :created_on, :datetime
column :updated_on, :datetime
validates_presence_of :abstract
validates_length_of :abstract, :within => 1…80
def save(identifier)
if self.valid?
#I set my stuff in a file……….
true
else
false
end
end
def before_create
self.created_on ||=Time.now
end
def before_update
self.updated_on ||=Time.now
end
end
Can you just use callbacks? Something like:
class PageMetadata < ActiveRecord::Base
before_create :set_created_on
before_update :set_updated_on
private
def set_created_on
self.created_on ||= Time.now
end
def set_updated_on
self.updated_on ||= Time.now
end
end
No, I can’t do that.
It’s like the callbacks didn’t work cause I am not calling to a normal
save() method, but a save(identifier) method…
Um…ok…I will try this then… So this is the same reason why I also
have to valid inside my new save() method? Cause the validations and the
errors added has to be also managed by myself
On Mar 17, 12:27 pm, Damaris F. <rails-mailing-l…@andreas-
s.net> wrote:
No, I can’t do that.
It’s like the callbacks didn’t work cause I am not calling to a normal
save() method, but a save(identifier) method…
You are overwriting the ‘save’ method entirely, which overwrites the
original behavior of save. The way ActiveRecord works the save method
will check to see if you are saving a new record or updating an
original, by calling “create_or_update”.
If you are creating a new record the ‘create’ method is called. This
method is actually aliased to ‘create_with_callbacks’, which invokes
the callback stack for before_create, etc.
You can specify the callbacks you want to use in the following
example:
def save(identifier)
if valid?
callback( :before_create )
# … write to disk
callback( :after_create )
end
#… other stuff…
end
OR you can overwrite the create/callback chain:
class YourModel
def create
# YOUR CREATE FUNCTIONALITY HERE
end
alias_method_chain :create, :callbacks
end
Now when you call save, your “create” method will be used over
ActiveRecord’s. Now you don’t even have to overrirde save, because you
can put your “save” logic in the create method. Likewise you’d want to
do the same for the “update” and “destroy” methods as well.
Zach
http://www.continuousthinking.com