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

**URL:** <https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458>\
**Category:** Rails\
**Created:** [June 15, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458 "2006-06-15T12:01:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 15, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/1 "2006-06-15T12:01:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 15, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/2 "2006-06-15T13:38:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 15, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/3 "2006-06-15T16:13:51Z")

</div>

On 6/15/06, enrique barraorion [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 15, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/4 "2006-06-15T12:58:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 19, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/5 "2006-06-19T15:29:52Z")

</div>

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 !@event.update_attributes(params[:event]) || !@event.save
  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

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 20, 2006, 1:12am UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/6 "2006-06-20T01:12:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 19, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/7 "2006-06-19T22:21:46Z")

</div>

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 :

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 20, 2006, 11:30am UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/8 "2006-06-20T11:30:35Z")

</div>

On 6/19/06, enrique barraorion [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

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

---

<div class="post-metadata">

**Author:** ![barraorion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@barraorion](https://www.ruby-forum.com/u/barraorion)\
**Post date:** [June 20, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/rails-saves-it-to-the-database-and-i-dont-want-to/63458/9 "2006-06-20T18:48:04Z")

</div>

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
