hello guys! I''ve just switched to RoR from Php and I see some bad habits are hard to abandon.. anyway I was used to ''print_r($obj)'' to check the structure of my objects/arrays and now I''m a bit lost cause I need something like: .. print self.id .. cause .. def self.menus @menus = Menu.find(:all, :conditions => [ "panel = ?", self.id]) end .. but INSIDE my menu.rb model.. the query is not working maybe cause ''self.id'' is out of scope, but I''m sure I could fix the problem with a bit of var_dumping.. :$ p.s. AnyOne else from Italy here ?? -- Emanuele ''meK'' Tozzato [mobile] +39 329 2956995 [skype] ''Emanuele meK Tozzato'' [icq] 30 77 52 76 [web] mekdigital.com [msn] mekdigital-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org
You can''t print/echo/puts from a model in rails; you can only send output from templates, or maybe from the controller in a pinch. There are better ways to debug in rails, though :) Here are a couple of debugging-related things you might be interested in knowing about: http://wiki.rubyonrails.com/rails/show/Console http://wiki.rubyonrails.com/rails/show/HowtoDebugWithBreakpoint I''d highly recommend that you buy and read "Active Web Development with Rails" -- it''s an excellent introduction to rails, and I found it to be very useful (I switched from php to rails a few months ago). It''s available at http://www.pragprog.com/. Tyler
> I''ve just switched to RoR from Php and I see some bad habits are hard > to abandon.. anyway I was used to ''print_r($obj)'' to check the > structure of my objects/arrays and now I''m a bit lost...> but INSIDE my menu.rb model.. the query is not working maybe cause > ''self.id'' is out of scope, but I''m sure I could fix the problem with > a bit of var_dumping.. :$I agree, breaking habits of several years is hard. You are not alone - many of us are also training ourselves to be better programmers. Rather than try to debug like that in the model (or view and controller, for that matter), you should set up a unit test to make sure that the model is performing correctly. You can find a manual on testing Rails code [1] on the RoR site. The Rails pragmatic programmer book also has a great section on testing. [1] http://manuals.rubyonrails.com/read/book/5 -- Jeremy Weathers Some mornings it just doesn''t seem worth it to gnaw through the leather straps. - Emo Phillips
On 2005.07.19., at 16:25, Emanuele Tozzato wrote:> > hello guys! > > I''ve just switched to RoR from Php and I see some bad habits are > hard to abandon.. anyway I was used to ''print_r($obj)'' to check the > structure of my objects/arrays and now I''m a bit lost cause I need > something like: > > .. > print self.id > .. >You could use the logger to print output to the development log, like so: logger.info "self.id: " + self.id More information here: http://wiki.rubyonrails.com/rails/show/logger Or, you could set a breakpoint, and run script/breakpointer to debug the session. More info here: http://laughingmeme.org/archives/002900.html http://wiki.rubyonrails.com/rails/show/HowtoDebugWithBreakpoint Or, you could run script/console - it will preload your models, you can test the functions via interactive ruby. Or :) you can use the debug view helper, which acts like print_r (only in the templates, though): <pre> <%= debug(whatever_object) %> </pre> You have a lot of options, as you can see :) cheers, Zoltan.
You could use: warn some_object.inspect In model, view, or controller to see what''s in an object ... output goes to WEBrick console. To see in browser you could set an instance variable in the controller: @variable = some_object then show the results in the view: <%= "<pre>#{@variable.inspect}</pre>" %> You want the <pre> tags since the output of "inspect" contains pointy brackets whose content the browser would otherwise hide.