Forum: Ruby on Rails Something wierd with .save method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
37c8f9e9c2318294bf934bd98568580e?d=identicon&s=25 anupam (Guest)
on 2007-05-04 14:04
(Received via mailing list)
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.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-04 15:59
(Received via mailing list)
>   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/...
http://rails.rubyonrails.org/classes/ActiveRecord/...

--
Rick Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
4c4c46ad9a5e4df8ba4ea8353e19c2fd?d=identicon&s=25 Curtis Edmond (Guest)
on 2007-05-04 16:15
(Received via mailing list)
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
37c8f9e9c2318294bf934bd98568580e?d=identicon&s=25 anupam (Guest)
on 2007-05-06 02:51
(Received via mailing list)
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
37c8f9e9c2318294bf934bd98568580e?d=identicon&s=25 anupam (Guest)
on 2007-05-06 03:26
(Received via mailing list)
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.
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-05-06 08:23
(Received via mailing list)
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
This topic is locked and can not be replied to.