# Something wierd with .save method

**URL:** <https://www.ruby-forum.com/t/something-wierd-with-save-method/103341>\
**Category:** Rails\
**Created:** [May 4, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341 "2007-05-04T14:04:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anupam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@anupam](https://www.ruby-forum.com/u/anupam)\
**Post date:** [May 4, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341/1 "2007-05-04T14:04:26Z")

</div>

Hi,  
I have a very sime app.  
I need to create as many records in the db as the selected  
checkboxes (named :isselected) in the view.

Here is the code snippet from the controller:

isselectedhash = params[:isselected]  
for mod in isselectedhash.keys  
@issue = Issue.new(params[:issue])

```
  @issue.state = 'NEW'
  @issue.mod_id = isselectedhash[mod] # this gets the mod_id

  logger.warn("saving ....")
  logger.warn(@issue)
  @issue.save

```

end

When I have two boxes selected this loop goes thru twice, but only  
one record is created in the DB.  
Here is the log,. Note, no SQL for the second object that needs to  
be saved. Why is that?

* * *

## Processing IssuesController#create Parameters: {“commit”=\>“Create”, “action”=\>“create”, “controller”=\>“issues”, “issue”=\>{“project\_id”=\>“1”, “description”=\>“desc”, “solution”=\>“desc”, “owner”=\>“qqqq”}, “isselected”=\>{“KSS2”=\>“2”, “KSS”=\>“1”}} saving … #Issue:0x690a58c [4;35;1mSQL (0.000000) [0m [0mBEGIN [0m [4;36;1mSQL (0.000000) [0m [0;1mINSERT INTO issues (`project_id`, `mod_id`, `number`, `date`, `description`, `solution`, `owner`, `state`) VALUES(1, 2, ‘1013’, ‘2007-05-04 07:49:46’, ‘desc’, ‘desc’, ‘qqqq’, ‘NEW’) [0m [4;35;1mSQL (0.031000) [0m [0mCOMMIT [0m added new issue KSS 1 saving … #Issue:0x6902a1c added new issue Redirected to [http://localhost:3000/issues/list](http://localhost:3000/issues/list)

Can anyone please help?

Thanks,  
Anupam.

---

<div class="post-metadata">

**Author:** ![anupam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@anupam](https://www.ruby-forum.com/u/anupam)\
**Post date:** [May 4, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341/2 "2007-05-04T16:15:45Z")

</div>

What you could do is add an exclamation mark to your .save call which  
would make it bark if the save isn’t getting executed for some reason.  
So instead of just @issue.save, it would be @issue.save!

That may point you in a certain direction, maybe that record isn’t  
valid for some reason.

Curtis  
[http://www.okwithfailure.com](http://www.okwithfailure.com)

---

<div class="post-metadata">

**Author:** ![anupam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@anupam](https://www.ruby-forum.com/u/anupam)\
**Post date:** [May 4, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341/3 "2007-05-04T15:59:39Z")

</div>

> When I have two boxes selected this loop goes thru twice, but only  
> one record is created in the DB.  
> Here is the log,. Note, no SQL for the second object that needs to  
> be saved. Why is that?

If a record doesn’t save, it means a validation failed, or a before\_\*  
callback cancelled it.

[http://rails.rubyonrails.org/classes/ActiveRecord/Validations.html](http://rails.rubyonrails.org/classes/ActiveRecord/Validations.html)  
[http://rails.rubyonrails.org/classes/ActiveRecord/Callbacks.html](http://rails.rubyonrails.org/classes/ActiveRecord/Callbacks.html)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![anupam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@anupam](https://www.ruby-forum.com/u/anupam)\
**Post date:** [May 6, 2007, 3:26am UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341/4 "2007-05-06T03:26:50Z")

</div>

I found the error. It was a logical error in my code! In order to get  
the error condition I had:  
dberror ||= issue.save  
(once I got a true from the first iteration, it never did the second  
save onwards)

I changed it to following and it works:  
dberror &&= issue.save

I would still like to know if there is a way to run rails in a debug  
mode to see the stack trace.  
Thanks,  
Anupam.

---

<div class="post-metadata">

**Author:** ![anupam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@anupam](https://www.ruby-forum.com/u/anupam)\
**Post date:** [May 6, 2007, 2:51am UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341/5 "2007-05-06T02:51:49Z")

</div>

Hi,  
Thanks for the pointers.  
I haven’t overriden any of the default methods of the  
ActiveRecord::Base class. I even tried to define them (validate\*)  
methods.  
Yet, I get the same result, only one record is created.

Any way to turn on debugging to see the entire stack trace?

Thanks,  
Anupam

---

<div class="post-metadata">

**Author:** ![anupam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9de053/32.png) [@anupam](https://www.ruby-forum.com/u/anupam)\
**Post date:** [May 6, 2007, 8:23am UTC](https://www.ruby-forum.com/t/something-wierd-with-save-method/103341/6 "2007-05-06T08:23:05Z")

</div>

anupam wrote:

> I found the error. It was a logical error in my code! In order to get  
> the error condition I had:  
> dberror ||= issue.save  
> (once I got a true from the first iteration, it never did the second  
> save onwards)
> 
> I changed it to following and it works:  
> dberror &&= issue.save

The save method just returns true or false, so I’m not sure what you’re  
gaining with this. The usual approach is to say:

if issue.save  
#do something  
else  
#…  
end

> I would still like to know if there is a way to run rails in a debug  
> mode to see the stack trace.

Rails has the concept of “run modes”: development, production and test.  
While you are developing, you run in development mode (that’s the  
default, so it’s probably what you are doing).

Rails logs to a log file with the name of the mode in the log directory.  
So, if you tail development.log, you should see any stack traces,  
queries, etc.

Also, if an exception is thrown while running in development mode, rails  
should give you an error page in the browser with the stack trace.

b
