Reload! rails app without closing rails console or objects instantiated within it? - ruby-on-rails

I am using Rails Console to help me muck around with an object instance running a Watir browser instance.
I've had to navigate via watir through various forms to get to where I want to thus I don't want to have to recreate the object and browser instance everytime I want to see the effects of changed code I'm wriring.
reload! doesn't seem to do the trick for me, confirmed for e.g. through changing proposed browser field text input in the code, calling reload! in the active rails console session and calling a method, the old text still used from before the reload!.
Is there a way to reload so that I can use uodated code on the already created objects in the already running rails console session?
Thanks

Related

Ruby on Rails Runtime Console

I am trying to find a way to get a rails console for a program during runtime with all instantiated variables. The normal rails console does not have access to any instantiated variables of its respective running application. For example, when a rails application crashes during runtime, a webpage will load with the error listed in red text, a snippet of the code where the error was raised, and a console at the bottom with access to variables instantiated during runtime.
See image below for console im talking about
The best thing I could find was a gem called pry, which seems to allow you to access a console during runtime by adding the line 'binding.pry' in your code at the point where you want access to the console. I would be fine with this, but seeing as how rails already gives you access to this when your app crashes, I would think there is a "vanilla" way to do this. Unfortunately I can't find anything online about this feature in rails. This seems like such a valuable tool for debugging I can't see why rails doesn't implement this. Is there a better way to debug during runtime? a better gem?
I will suggest you to use Better Errors
Better Errors replaces the standard Rails error page with a much better and more useful error page. It is also usable outside of Rails in any Rack app as Rack middleware.

How to make rails code reloaded each request

I'd like to play with rails code to understand it deeper and how everything works internally. And it could be very nice to change rails code and see the changes after reloading page (by default I need to restart server), what is the right way?
you shouldn't need to restart your server just refresh your page, except for adding migrations
also you can play with your rails app interactively using the rails console similar to irb
just type rails console or rails c into your app directory

Data in Database not reflected by Rails calls

This is the strangest bug I've ever encountered
I am using the Best In Place gem in my Rails app in order to allow in-place editing of a page title. The in-place editing works, and the new title gets changed in the database, but when I refresh the page, it reverts back to the old title. I don't even understand where it's getting the old title from, since it's not stored in the database anymore.
When the page is created, it is automatically given the title "Untitled Page". When I change the title to say, "Title", and look at the row for the page in the DB with the postgresql admin program, it does indeed change to "Title". But when I run Page.find(1).title in the rails console, it returns "Untitled Page"
How could this be?!
Is your app running in production mode?
The PG response and console response cannot be different unless they are being executed on two different databases.
Check that your database config is using the same database as the one you are manually connecting to when browsing PG.
Ensure that when you run the rails console you specify the environment (in case your default is not what you are running on):
$ rails c production
$ rails c development
If both of the above don't help, please post the log snippet of this action. Might be that the transaction is being rolled back. If you are using .save or .update_attribute without the "!" then this won't throw an error. This is highly unlikely though, since you are saying that the database has updated data.
Fixed it. The reason was that I was using after_initialize to set the automatic properties, which gets called whenever the ActiveRecord object gets initialized. What I really wanted was after_create. I use after_create to call a function called set_properties, where I set things like self.title, and then at the end of the function, I call self.save, which is required for the properties to get saved into the DB.

If you modify code in the Rails console will that affect a server running in parallel?

Is it possible to run "rails console" in one shell and then "rails server" in another and then have code changes in the console permeate to the running application? Presumably this isn't possible, but I'd just like to check if there is a way.
Edit: Both are running in the same environment. And by code changes I mean changes to class definitions (e.g. rewriting a method on the Post model).
If you modify any data, that will indeed permeate. However modifications to methods done on the fly by opening classes and "monkey-patching" them will not affect your running application - unless your modified method modifies data.
However, it is always advisable to run the console in a different environment with different data to avoid harming a running application.
If you are changing data in your console IN THE SAME ENVIRONMENT then it will be changed in the browser.

Is the rails console dynamic?

is the console in rails (~ rails c) dynamic? For example; if I open the console and then make changes to a model will it pick these changes up or do I have to exit out of the console and run rails c again for it to pick up the changes in the model?
You will need to call the reload! method in the console to reload the changes. This method's magic is automatically called by rails server in development mode.
As a comment's pointed out beneath and another answer here, if you change things to do with the environment of the application, such as adding new gems to the Gemfile, making changes to anything in config or adding a new plugin then you'll need to restart the console. Any changes to app will be reloadable with reload!
If you were using this particular way to test that a method was working, I wouldn't. Tests (as in, the Test::Unit or RSpec) variants are much nicer because you have a reproducible way of running them again and again. rails console is great for one-off testing, but if you want to write a maintainable application then write tests.

Resources