Rails saves it to the database and I don't want to

Hello:
I am developing a web application to book machines for an event.
so I have an event model
class Event < ActiveRecord::Base
has_many :datetimes

end
and a datetime model
class Datetime < ActiveRecord::Base
belongs_to :event

end

I have developed a view with the event information and information for
the datetimes. and in the event_controller when I capture a datetime I
do
@datetime = params …
@event.datetimes << @datetime.

My problem is that “@event.datetimes<<@datetime” saves the datetime to
the datetimes table in the database. And after that when I validate the
event for the datetimes not to overlap if I return an error the bad
datetime is already saved!!
I don’t want the datetime being saved when I introduce it in the
database
thanks in advance

I have changed the datetime model for one called event_datetime and it
still happens, rails saves the datetimes to the database each time I do
a
logger.debug(“hello before”)
@event.event_datetimes << @datetime
logger.debug(“hello after”)

the logs are:
hello before
SQL (0.001268) BEGIN
SQL (0.000426) INSERT INTO event_datetimes (end_date, event_id,
start_date, at_job) VALUES(‘2006-03-15 01:18:00’, 20, ‘2006-03-15
00:18:00’, 0)
SQL (0.002012) COMMIT
hello after

any other ideas?

On 6/15/06, enrique barraorion [email protected] wrote:

SQL (0.000426) INSERT INTO event_datetimes (end_date, event_id,
start_date, at_job) VALUES(‘2006-03-15 01:18:00’, 20, ‘2006-03-15
00:18:00’, 0)
SQL (0.002012) COMMIT
hello after

any other ideas?

Try wrapping the whole update in a transaction.

Isak

enrique barraorion wrote:

I am developing a web application to book
machines for an event.
so I have an event model
class Event < ActiveRecord::Base
has_many :datetimes

end
and a datetime model
class Datetime < ActiveRecord::Base
belongs_to :event

I think you need to rename your model to something other than Datetime.
Perhaps EventDateTime. RoR already has a Datetime class and you’re
overriding it.

hth,
Bill

It does not work. This is my update method:
def update
@event = Event.find(params[:id])
indice = 0;
param_name = ‘datetime’ + indice.to_s

array_datetimes=[]
while params[param_name.to_sym]
  @datetime = EventDatetime.new(params[param_name.to_sym])
  is_valid = "is_valid_time" + indice.to_s
  if(params[is_valid.to_sym]=="true")
      array_datetimes << @datetime
  end
  indice+=1
  param_name = 'datetime' + indice.to_s
end

#this two following lines save the event_datetimes to the database
#and I don't want to
@event.event_datetimes=[]
@event.event_datetimes << array_datetimes.reverse
if [email protected]_attributes(params[:event]) || [email protected]
  render :action => 'edit', :id => @event
else
  flash[:notice] = 'Event was successfully updated.'
  redirect_to :action => 'show', :id => @event
end

end

I think the problem can be that I do “@event = Event.find(params[:id])”,
this takes the event from the database and when I change event_datetimes
(that is in another table) It saves it. This does not happen if instead
Event.find(params[:id]) i do “@event = Event.new” then
@event.event_datetimes << array_datetimes.reverse won’t save it to the
database, and @event.save will do that but with another id, and if I do
@event.id = params[:id] mysql throws an exception.
i am desperate, I need to update the event but does not work. Does this
happen allways, I mean that rails saves the attributes to the database
instead of just saving it to the object an to the database when doing
save?
thanks

I reset the event_datetimes with @event.event_datetimes=[] because if I
don’t do that when I do “@event.event_datetimes<<@datetime” it saves me
another entry of the datetime in the array and if I have 2 entries when
I update I have 4 an after that 8 an so on.
@event.save really update the attributes of the event but no the tables
that are related to the event
this is the log for @event.save
UPDATE events SET service = ‘TeleMeeting’, quality = ‘512K’, uri =
‘’, description = ‘20’, password = ‘20’, name = ‘20’ WHERE id = 4
and I also can do a “@event.event_datetimes[0].save” and it updates the
event_datetimes entry

I am starting to think that the problem is related to the << operator,
that adds a new datetime to the array and rails saves it to the
database, but I don’t like this behaviour because if the user introduces
an entry that is no a valid one when I validate and return an error
rails has already saved it to the database

hello enrique,
I will try to help you…
I don’t really understand why rails save your event_datetimes yet but
there are something I am not sure in your code …maybe you can try…
well, first:
if you do an update of the event,
why do you reset the event_datetimes array @event.event_datetimes=[], in
a has_many the @event.event_datetimes will return the array of
event_datetimes …(since you are in an update process)
=> don’t reset your array and just:

@event.event_datetimes << array_datetimes.reverse

If it still does not work, please tell me since i am working on a same
database model I will try with my model…

arnaud

enrique barraorion a écrit :

On 6/19/06, enrique barraorion [email protected] wrote:

It does not work. This is my update method:
def update
@event = Event.find(params[:id])
snip
happen allways, I mean that rails saves the attributes to the database
instead of just saving it to the object an to the database when doing
save?
thanks

“If you wish to assign an object to a has_one association without
saving it, use the association.build method (documented below).” -
snipped from this page:

Or you could use transactions…

def update
AnyARModel.transaction do
create and save! stuff here… if there’s an error, the whole block
is rolled back.
end
end

GL,
Isak

It has worked!!
thank you very much to all of you. My problem using transactions was
that I was using save instead of save! and it did not launched an
exception and finished the transaction with a commit instead of
rollback.

just for your information associations worked the same way as <<, build
added new entries to the array but it did not update the ones that were
there before

thank you all