Disconnect the database in Ruby on Rails - ruby-on-rails

I have a Ruby on Rails data mining app, and I would like to be able to do powerful demonstrations without a complicated user-interface.
Is there any way I can make a ruby console for the web that doesn't run the risk of say...
User.destroy_all
?
Something where I can call
ActiveRecord.disconnect!
unsafe_actions
ActiveRecord.connect!
But where I could also have read priviledges (select, inner join, etc.)
Thanks,
Brian

You can run the rails console in sandbox mode:
rails console --sandbox
or
rails c -s
Any changes you make in sandbox mode will be rolled back upon exiting the console.
(And prefix with heroku run to run it on heroku.)

Related

Is there a way to log commands from Rails console in Heroku?

Hi I am using Heroku and Ruby on Rails. In heroku I can do
heroku run rails c -a app
Which gives me console access, and I can do administrative things like deleting records
I am working in a company setting and I would like to have a log of all commands entered in the console. So if something is wrong, we can audit the logs.
Is there a way to record what is typed in the rails console?
Rails allows you to store the console history but this won't be saved in Heroku, at least not permanently.
In Heroku, when you run heroku run rails console Heroku will create a one-off dyno which will only live as long as the console session. Rails will store the history of commands on the filesystem but once the console is exited the dyno will be cleaned up and the command history file will be deleted along with it.
Rails console is a Railtie and it has some lifecycle hooks, so you could come up with some custom way to persist commands, but there is no built-in way to do it.
Yes, you can now using Heroku's shield private spaces, https://elements.heroku.com/addons/heroku-private-spaces.

what's the easiest way to setup a ruby/rails sandbox locally?

I like to play with dynamic programming languages before I get confident regarding their dynamic behaviors. While there are online ruby sandbox sources available, I prefer to test it out locally.
In javascript a html file with script tag is sufficient to write any sample javascript code.
What's the equivalent of forming this sandbox for ruby with...?
1. a boilerplate
2. manually setting up ruby sandbox (prefered)
If you just want a ruby sandbox $ irb will do the trick.
If you want a ruby on rails sand box $ rails c -s short for $ rails console ––sandbox.
This command loads our Rails application, connects to the database and automatically starts a database transaction. All database operations performed within this console session are rolled back upon leaving the console.
Install ruby https://www.ruby-lang.org/en/documentation/installation/
And use Interactive Ruby Shell(IRB) https://www.ruby-lang.org/en/documentation/quickstart/
BitNami is by far the easiest setup for a local installation:
https://bitnami.com/stack/ruby

Want to develop rails site offline then move to server

Is there an issue with developing my site on my macbook and then moving to a server when done? Will there be issues I need to plan ahead for? DB or ruby related maybe? Dependencies or something a server could have different from my dev environment that could cause a nightmare later? I'd rather develop it offline since it'd be faster and wouldn't require an internet connection but in the past I've always done everything with live sites so this would be a first, and I am new to ruby on rails.
Developing locally and then deploying to your server(s) via something like capistrano is standard practise.
It's a good idea to keep your development environment as close as possible to your production environment (ruby versions, database versions etc). Bundler makes keeping your gems in sync easy
I used Heroku for some projects. The deployment was as easy as it could be. I just did a git push and it worked without problems... I really like bundler and rake :-)
Your Question embodies THE way to develop in Rails. Your development environment is an offline representation of what you're production site will be.
A quick workflow analysis for you could be:
rails new ~/my_app -d postgresql; cd ~/my_app; rm public/index.html
Next, create the database:
bundle exec rake db:create:all
Now you'll have the db and app all set up, let's set up your main pages:
bundle exec rails generate controller Site index about_us contact_us
Now you'll have something to see on the site, so run:
bundle exec rails server
This server acts as your offline connection and will handle the rendering of any text, images, html etc you want to serve in your rails app. Now you can join in the debates of TDD, to TATFT or JITT, rspec vs test::unit. Welcome.
Developing locally is definitely the way to go. However, I would look into getting it on production as soon as possible and pushing often. This way you can see changes happen as you make them and are aware of any possible breaking changes.
I use heroku a lot and when I start a new project I push it to heroku almost immediately. While developing, I can publish new changes simply by git push heroku master. Everyone has to find their own workflow, but this has always worked well for me.
If you are interested in Heroku here is a good link to get you started:
https://devcenter.heroku.com/articles/rails3

How to tell when Rails app is activated by migration?

I'm trying to create a migration for my app, and in this app I'm using a gem that tries to startup a different service upon app startup. Apparently, creating a migration...
rails generate migration AddSomeStuffToTable stuff:string
...activates the app, and this gem which tries to connect to startup the service. It appears that starting up the app via generating a migration makes the service startup unable to connect, so it just keeps sleeping and trying again, never actually running the migration.
In this gem, I've already dealt with this for rake, so this is what I've got so far:
MyService.start unless defined? Rake or defined? IRB
This handles the rake problem (like rake db:migrate, rake db:populate), but how can I handle the creation of migration, which (as far as I know) is not a Rake task?
You could try using environment variables for disabling the service:
MyService.start unless ENV['NO_SERVICE']
And run your command like this:
NO_SERVICE=1 rails generate migration AddSomeStuffToTable stuff:string
However, I doubt this scales well, especially if multiple developers are in the app. A better approach might be to do the reverse of this, to only start the service if a particular env variable is present. However, going this direction, you'd need to make sure your app servers set this variable, for example:
Apache: SetEnv START_SERVICE 1
nginx: env START_SERVICE=1
thin: START_SERVICE=1 thin start

My Rails console session went wrong and now my rspec tests fail?

I am using Postgres 9.1, Rails 3.1.3 and Ruby 1.9.3.
I tried some things out in rails console --sandbox and then I ventured to rails console.
On entering Employee.create!(a whole lot of data), I got some errors, most of them to do with not null constraints.
I quickly left the console and using pgadmin3 checked to see if I had changed the database but there was no data so I felt confident that my tests would still pass. Unfortunately, my rspec test now fails and I am not sure where the conflicting data is.
Hopefully someone reading this has an idea or suggestion of how I can undo the entry I entered in my console session, which unfortunately was not the --sandbox session.
I did manage to kill the phantom server which I found through lsof | grep 3000.
Thanks for any help, suggestions or pointers you can spare.
When you play in the console, you're in the development environment, I don't think there is a way that it conflicts with your tests which are run in the test env.
Plus, the tests should not rely on any existant data, since the test database is erased each time you run your tests.
Try to run rake db:test:prepare and run your tests.

Resources