I''m trying to initialize an AR model with some default values
(populating it and some other models when the first model is
created). I''ve tried putting the code in the model''s
initialize
function, to no avail. Right now I have:
# (app/models/event.rb)
def initialize
Event.transaction do
max_item = Event.find(:first, :order => "list_order DESC")
max_num = 0
if max_item
max_num = max_item.list_order
end
list_order = max_num + 1
end
super
unless args = []
default_seat_groups
end
end
####################
This throws various errors on trying to save the model.
default_seat_groups is the method that sets up an associated model
(seat_group belongs_to :event). I haven''t been able to find docs on
the best way to initialize a model with arbitrary code. Am I thinking
about this the wrong way?
Thanks!
--
Brad Ediger
866-EDIGERS
Hello Brad, The way I do it is: def initialize(args=[]) super(args) # Default values, but only if not already initialized self.category = Category.default if self.category.blank? end You can''t access an object''s attributes until super''s been called, because super will query the DB table (SHOW FIELDS FROM model) and only subsequently will make the accessors available. In your specific case though, I don''t see why it doesn''t work. What errors do you get ? Hope that helps, François Brad Ediger said the following on 2005-08-29 20:45:> I''m trying to initialize an AR model with some default values > (populating it and some other models when the first model is created). > I''ve tried putting the code in the model''s initialize function, to no > avail. Right now I have:
François,
Thanks for your help. Here''s what I have now:
def initialize(args=[])
super(args)
# Assign list order
Event.transaction do
max_item = Event.find(:first, :order => "list_order DESC")
max_num = 0
if max_item
max_num = max_item.list_order
end
list_order = max_num + 1
end
unless args == []
default_seat_groups
end
end
The error this gives me is:
NoMethodError in Events#new
undefined method `stringify_keys!'' for []:Array
That error happens on the super(args) line. I''ve been getting various
errors throughout this, but I can''t seem to figure out anything
meaningful. It occurs when accessing localhost:3000/events/new to
create a new event.
Thanks again,
Brad
On Aug 31, 2005, at 8:17 AM, François Beausoleil wrote:
> Hello Brad,
>
> The way I do it is:
> def initialize(args=[])
> super(args)
>
> # Default values, but only if not already initialized
> self.category = Category.default if self.category.blank?
> end
>
> You can''t access an object''s attributes until
super''s been called,
> because super will query the DB table (SHOW FIELDS FROM model) and
> only subsequently will make the accessors available.
>
> In your specific case though, I don''t see why it doesn''t
work.
> What errors do you get ?
>
> Hope that helps,
> François
>
>
> Brad Ediger said the following on 2005-08-29 20:45:
>
>> I''m trying to initialize an AR model with some default values
>> (populating it and some other models when the first model is
>> created). I''ve tried putting the code in the model''s
initialize
>> function, to no avail. Right now I have:
>>
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Oh my, I''m sorry, I did a booboo... Brad Ediger said the following on 2005-09-01 14:52:> def initialize(args=[]) > super(args) > *NoMethodError in Events#new*That should have been: def initialize(args={}) # code end Notice we receive a Hash, not an Array. Why oh why didn''t I just copy some code over ? Must be all that DRY stuff from the PragProg and RoR :) Bye ! François
Aah -- that''s right! Anyway, I made that same mistake independently (Array instead of Hash) when I was trying to fix things. I''ll try it out! Thanks, be On Sep 2, 2005, at 7:24 AM, François Beausoleil wrote:> > That should have been: > def initialize(args={}) > # code > end > > Notice we receive a Hash, not an Array. > > Why oh why didn''t I just copy some code over ? Must be all that > DRY stuff from the PragProg and RoR :)