Something wierd with .save method

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

Can anyone please help?

Thanks,
Anupam.

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

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/Callbacks.html


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

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.

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

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