How can I get rails to use pg by default? - ruby-on-rails-3

It's not a huge burden but I would really like to be able to change the default gem-set on my rails apps when I create them so that they're ready for Heroku.
What is the best way to go about doing this?

You can specify the database with -d when running rails new:
Usage:
rails new APP_PATH [options]
Options:
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
Description:
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
So for PostgreSQL this is:
rails new myapp -d postgresql
To make this the default put -d postgresql into ~/.railsrc
Another option is to change gem 'sqlite3' to gem 'pg' in your Gemfile as suggested in Getting Started with Rails 3.x on Heroku.

Related

Issues with setting PostgreSQL up and db:rake

I've had numerous issues with setting up Postresql for a project I'm working on.
Some information:
-I'm running Ubuntu 12.04
-I have a postgres user account installed
-I'm trying to use a project in RoR.
I'm trying to use this command:
"bundle exec rake db:create"
I tried using it as sudo, computer user, and postgres.
When I run it as my computer user it returns "fe_sendauth: no password supplied" and then aborts the rake.
I know my (user account) ruby version is 2.1.0 because when I run "ruby --version", it returns
"ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]"
However, when I run "ruby --version" as postgres, it returns
"ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]"
How can I have separate ruby versions for my user account and postgres? I tried using sudo to change the ruby version with RVM, but I was denied permission.
I don't remember setting the password for postgres sudo, and I cannot figure it out.
What can I do to
Recover/reset my sudo postgres password.
Install/set the newest (2.1.0) version of ruby on the postgres account once I have done step 1.
Run bundle exec rake db:create after all of that?
Hm, not sure any specific issues with linux, but it sounds like you have it installed okay.
Hard to say without looking at your database.yml, but the rails convention seems to be to connect to the database with the username <%= your_project_name %> and a blank password. Based on the error, you may not have created such a user/role in postgres.
# enter the psql shell as the postgres admin user
psql -d postgres
CREATE ROLE <%= your_project_name %> WITH LOGIN CREATEDB SUPERUSER;
\q
# create the dbs and load the schema
bundle exec rake db:create
bundle exec rake db:schema:load
bundle exec rake db:migrate
bundle exec rake db:test:prepare
# revoke superuser, it was just there in case there were any extensions that needed installing
psql -d postgres
ALTER ROLE <%= your_project_name %> WITH NOSUPERUSER;
\q
That should do that trick, although it sounds like you might be having issues with RVM as well. There is a ton of documentation on RVM on the website.
You also may be confusing the concept of users/roles in the DB with the concept of users in the OS. You should be able to access the database with any unix user but you need to authenticate rails into the database using database.yml. As noted above, there seem to be defaults that most rails projects i've seen use, but of course you could use whatever you want.
Ok, so I fixed my main issue with my ruby version being incorrect and not being able to fix it because I did not have authentication.
I did this with
$ /bin/bash --login
This let me then type
$ rvm --default use 2.1.0
This set my ruby version in user postgres to 2.1.0.
However, I then tried to run
$ bundle install
in user postgres, and I was denied and given this error
Unfortunately, a fatal error has occurred. Please see the Bundler troubleshooting documentation at
http://bit.ly/bundler-issues. Thanks!
/home/myusername/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/fileutils.rb:250:in `mkdir': Permission denied # dir_s_mkdir - /home/myusername/.bundler (Errno::EACCES)
I cannot access sudo for user postgres because I did not set the password for it. therefore, I believe that the answer to my question is to run
$ sudo bundle install
but I do not have the sudo password.
Any help with this issue would be appreciated.

How to rename rails 4 app?

rails plugin install git://github.com/get/Rename.git will allow us to rename only rails 3 app
Is there any gem available to rename Rails 4 app.
If not, suggest me the better way to rename.
Since rails 4.1.x, if you want to rename your application, the only two files you need to modify are config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module YourApplicationName # <-- rename it here
class Application < Rails::Application
...
end
end
and config/initializers/session_store.rb (optional):
# Be sure to restart your server when you modify this file.
Rails.application.config.session_store :cookie_store, key: '_your_application_name_session' # <-- rename the key
For Rails 4.0.x you can use the rename gem and execute the following command:
rails g rename:app_to New-Name
This will update the necessary files for you:
old/ (master) › rails g rename:app_to new
Search and replace module in to...
gsub config.ru
gsub Gemfile
gsub Gemfile.lock
gsub Rakefile
gsub README.md
gsub config/application.rb
gsub config/boot.rb
gsub config/environment.rb
gsub config/environments/development.rb
gsub config/environments/production.rb
gsub config/environments/test.rb
gsub config/initializers/backtrace_silencers.rb
gsub config/initializers/filter_parameter_logging.rb
gsub config/initializers/inflections.rb
gsub config/initializers/load_class_extensions.rb
gsub config/initializers/mime_types.rb
gsub config/initializers/secret_token.rb
gsub config/initializers/session_store.rb
gsub config/initializers/update.rb
gsub config/initializers/wrap_parameters.rb
gsub config/routes.rb
gsub config/initializers/session_store.rb
Renaming references...
Renaming directory...Done!
New application path is '/Users/username/code/new'
Add
gem 'rename' to Gemfile
then
bundle install
After that
rails g rename:app_to name_of_app
And if you are using mongoid then you need to rename the database name in config/mongoid.yml
There are two ways:
1 . Manually (For Rails 4.1.x)
You need to manually find the references to the application name. And you need to change them manually. Here is some common places where it is used:
config/application.rb
config/environment.rb
config/environments/development.rb
config/environments/production.rb
config/environments/test.rb
config/initializers/secret_token.rb
config/initializers/session_store.rb
config/routes.rb
config.ru
app/views/layouts/application.html.erb
Rakefile
2 . Automatic (For Rails 3 and 4.0.X)
Or you can use the rename gem and execute the following command:
rails g rename:app_to New-Name
For Rails 5
Require
config/application.rb change the module name
Optional
config/initializers/session_store.rb (in Rails.application.config.session_store) change the session name
app/views/layouts/application.html.erb You can change the <title>...</title>, if it is not already done
I just used this rename gem in a basic rails 4 app:
https://github.com/morshedalam/rename
This is quite a bit different to get's version.
Easy enough to use:
Add this to Gemfile:
gem 'rename'
And run:
rails g rename:app_to NewName
Seemed to the trick,
It also updated my rubymine .idea project settings :)
In Rails 4.2 just change in application config file
config/application.rb
and config/initializers/session_store.rb (optional):
Rails.application.config.session_store :cookie_store, key: '_your_application_name_session' # <-- rename the key
then restart your server.
That's it!
For rails 5.2
Add gem 'rename' to the gemfile
bundle install
rails g rename:into your_new_app_name
Here is a gem specifically for Rails 4
https://github.com/negativetwelve/rails-rename
(I haven't used it but it seems fine)
The other gems listed here only target Rails 3
In Rails 5.x, doing it manually
using ag (https://github.com/ggreer/the_silver_searcher), there are files that use the default folder name (if generated via rails new .)
three basic files, relative to root of project, need to be updated:
config/cable.yml > channel_prefix:
config/environments/production.rb > # config.active_job.queue_name_prefix
package.json > name
There are sure to be more locations as mentioned previously such as database etc. Hope this helps.
How to Rename a Rails 6.1.3 Application
the "rename" gem doesn’t appear to be tested against Rails 6 at this time according to their documentation so I opted for the manual route.
config/database.yml (rename the 3 databases)
config/application.rb (rename the module)
package.json (rename the package name)
README.md (in case it mentions your old app name)
app/views/layouts/application.html.erb (rename the title)
config/cable.yml (name the Channel_prefix database to the new production database)
Run npm install so that the name attribute you previously updated in package.json is registered.
Run your migrations rails db:migrate
Start your server rails s
For Rails 6.1 Here is what I did:
rename module config/application.rb
run bundle install
and run rails db:migrate.
after that running rails c then Rails.application.class.module_parent.name

rake db tasks disappear in deployment

I have a Rails 3.2.8 app which is running nicely in development on my machine, with Postgres as the db backend for ActiveRecord. However, when I deploy my code to an EC2 server, either via capistrano or by rsync'ing my project to the EC2 instance, rake no longer lists any of the db: tasks:
$ rake -T
rake about # List versions of all Rails frameworks and the environment
rake assets:clean # Remove compiled assets
rake assets:precompile # Compile all the assets named in config.assets.precompile
rake doc:app # Generate docs for the app -- also available doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-t...
rake load_gml # Load GML source files into the environment database
rake log:clear # Truncates all *.log files in log/ to zero bytes
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
rake rails:update # Update configs and some other initially generated files (or use just update:configs, update:scripts, or u...
rake routes # Print out all defined routes in match order, with names.
rake secret # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie se...
rake stats # Report code statistics (KLOCs, etc) from the application
rake test # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profil...
rake test:recent # Run tests for {:recent=>"test:prepare"} / Test recent changes
rake test:single # Run tests for {:single=>"test:prepare"}
rake test:uncommitted # Run tests for {:uncommitted=>"test:prepare"} / Test changes since last checkin (only Subversion and Git)
rake time:zones:all # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter,...
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sock...
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
rake -P also reports no db: tasks.
I'm guessing it's some sort of dependency problem, but I can't see what that would be since I'm declaring all my gem dependencies in a Bundler Gemfile. Postgres 9.2.1 is installed and working, but even if it wasn't I don't get why the rake task would vanish.
Any clues as to what's going on gratefully received!
And it turns out to be stupid-simple: I had copied the deploy.rb file from a previous project which didn't use ActiveRecord, so I hadn't set the role db:. I'm still not sure why that meant that the rsync'ed copy of my project also didn't have any rake db: tasks, but I'm not going to spend any more cycles worrying about that.

$ rails server... undefined method `gem' for main:Object (NoMethodError)

I'm new to Rails trying to complete this tutorial, but I can;t even get started.
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-rails_server
I am at Step 1.2.5
I am supposed to start the server using: $ rails server
I'm getting this error whenever I try to run any rails command.
localhost:first_app home$ rails server
/usr/bin/rails:22: undefined method `gem' for main:Object (NoMethodError)
Running rails commands used to work, because I had completed other tutorials from other books in the past. However, somewhere along the steps in this tutorial something was messed up.
Is it possible that editing my bash_profile caused this?
My bash_profile looks like this now:
export PATH=/bin:/usr/bin:"$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
I added this line to it: export PATH=/bin:/usr/bin:"$PATH" after following directions in step Listing 1.1. Creating a gem configuration file.
I'm not sure if this helps, but If I type:
$ which rails
I get:
/Users/home/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/bin/rails
Have you installed gem? type:
which gem
and see what you get. If you don't have gem or the wrong version, follow these instructions: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-install_rubygems
typing
$ rvm use 1.9.3#rails3tutorial2ndEd --create --default.
allowed me to continue.
I had a similar problem under ubuntu. I fixed it by changing gnome-terminal settings https://rvm.io/integration/gnome-terminal

apn_sender gem daemon not running

Im using apn_sender for rails 3 and i have been able to install the gem and get it working just fine by using
rake apn:sender
I have tried to get it started in production mode on a ubuntu box by starting the daemon and it does not seem to work. When i type
script/apn_sender --environment=production --verbose start
I dont see anything. No log present.
when i try to type
script/apn_sender status
It returns with
apn_sender: no instances running
Just trying to understand why it is not running.
i just solved this problem. Try to create a file called 'script' in generators/apn_sender/templates . .
Put this in your script file
# !/usr/bin/env ruby
# Daemons sets pwd to /, so we have to explicitly set RAILS_ROOT
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require 'rubygems'
require 'apn'
require 'apn/sender_daemon'
APN::SenderDaemon.new(ARGV).daemonize
bash 'rails g apn_sender' in your terminal and will create 'script/apn_server' with same content as above
After that bash this code
./script/apn_server --environment=production --verbose start
it will create log/apn_sender.log . Try running
APN.notify('token',{:alert => '#' , :badge => 1})
or else in rails c to confirm if it works or not , and of course
rake apn:sender
Hope it will help :)
EDIT
You have to install redis and configure