Damaris Fuentes
2007-Mar-17 13:01 UTC
Created_on and updated_on in a non-ActiveRecord model
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Mark Dodwell
2007-Mar-17 16:03 UTC
Re: Created_on and updated_on in a non-ActiveRecord model
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Damaris Fuentes
2007-Mar-17 16:27 UTC
Re: Created_on and updated_on in a non-ActiveRecord model
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... :( -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 17, 12:27 pm, Damaris Fuentes <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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Damaris Fuentes
2007-Mar-17 19:19 UTC
Re: Created_on and updated_on in a non-ActiveRecord model
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 :( -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Possibly Parallel Threads
- AR many-many join tables - can they have created_on, updated_on ?
- created_on & updated_on - helper to display date only
- override created_on column name
- How to write manage created_on and created_by via mixin?
- created_on, created_at defaulting to 2000/01/01 00:00:00