Can not create a Rails Resource using ZSH - ruby-on-rails-3

I'm trying to create the following resource in a Rails application
resources :sessions, only: [:new, :create, :destroy]
However, when I run this command I get the following error
zsh: bad pattern: [:new,
What could be causing this error?

That's not a command, that's an entry in a routes.rb file.
Edit the file instead of trying to run it from the command line; no more command line error.

Related

Cannot run elastic search in circleci to make my rspec for elasticsearch to pass?

i have configured my project in github with circleci. I am using searchkick for elastic search.
my code to reindex looks like this:
IN spec_helper.rb
RSpec.configure do |config|
....
config.before :each do
Location.reindex
end
...
end
My app/models/location.rb looks like this
class Location < ActiveRecord::Base
...
searchkick word_start: [:location_name], autocomplete: [:location_name]
...
end
My app/models/search.rb looks like this
class Search < ActiveRecord::Base
...
def self.location_auto_complete(search_term)
location_ids = Organization.locations.map(&:id)
Location.search(search_term, fields: [{location_name: :word_start}, :from, :search_id], where: {id: location_ids})
end
end
My /spec/models/search_spec.rb looks like this
require 'rails_helper'
describe Search, type: :model do
before :each do
#location = create(:location,location_name: 'kathmandu')
#location.reindex
Location.searchkick_index.refresh
end
describe '.location_auto_complete(search_term)' do
it 'return location search result for the location search term' do
expect(Search.location_auto_complete('kathmandu')).to match_array(#location)
end
end
My /myapp/config/circle.yml looks like this i followed : https://circleci.com/docs/installing-elasticsearch :
database:
override:
# replace CircleCI's generated database.yml
- cp config/database.yml.ci config/database.yml
- bundle exec rake db:drop db:create db:migrate
machine:
services:
- elasticsearch
dependencies:
post:
- wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.0.1.tar.gz
- tar -xvf elasticsearch-1.0.1.tar.gz
- elasticsearch-1.0.1/bin/elasticsearch: {background: true}
dependencies:
cache_directories:
- elasticsearch-1.0.1 # relative to the build directory
post:
- if [[ ! -e elasticsearch-1.0.1 ]]; then wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.0.1.tar.gz && tar -xvf elasticsearch-1.0.1.tar.gz; fi
- elasticsearch-1.0.1/bin/elasticsearch: {background: true}
When i run rspec in my laptop it works fine while i push to github(which is integrated with circleci)i, status is failed. It also says:
We didn't find a circle.yml for this build. You can specify deployment or override our inferred test steps from a circle.yml checked in to your repo's root directory.More information in our docs.
The error message says circle.yml should be in the repo's root directory, but you have it the config directory.

"Could not connect to server: No such file or directory" While deploying Rails app with beanstalk

While deploying an application with Rails 3.2.17, we are getting following error.
eb-commandprocessor.log
+ su -s /bin/bash -c 'leader_only bundle exec rake db:migrate' webapp
rake aborted!
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
/var/app/ondeck/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace) (ElasticBeanstalk::ActivityFatalError)
at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:189:in `rescue in exec'
...
caused by: command failed with error code 1: /opt/elasticbeanstalk/hooks/appdeploy/pre/12_db_migration.sh
Here is our database.yml configuration
production:
adapter: postgresql
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
Surprisingly we are able to start rails console and we also verified that all above environment variables return correct value.
And also able to connect via psql with following command
psql -h RDS_HOSTNAME -d RDS_DB_NAME -U RDS_USERNAME
On my side the issue was coming from the fact that I deleted my .elasticbeanstalk/optionsettings and so RACK_ENV was not defined, AWS set it by default to production which led my application to have the wrong database hostname.
I debugged that by adding echo statement in /opt/elasticbeanstalk/hooks/appdeploy/pre/12_db_migration.sh to display RACK_ENV and noticed it was empty.

Rails 3, rake db:test:prepare doesn't work when using a postgres Schema

I have a Rails 3.2.1 project that I'm trying to run my specs on.
When I run:
rake
rake aborted! PG::Error: ERROR: invalid value for parameter
"search_path": "example" DETAIL: schema "example" does not exist :
SET search_path TO example
From what I can work out, rake db:test:prepare drops the test database and then attempts to recreate it from the schema.rb. The thing is, database.yml has the line
schema_search_path: example
so when it tries to reconnect, it fails with the above error.
I think my question is, how can I get rake db:test:prepare to setup the schema_path too?
One solution is to add a bit of code to the db:create task that creates the schema. Put this in a rake task, for exmaple 'APP_ROOT/lib/tasks/db_create_schema.rake'
namespace :db do
task :create do
config = Rails.configuration.database_configuration[Rails.env].merge!({'schema_search_path' => 'public'})
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute("CREATE SCHEMA schema_name")
end
end
https://gist.github.com/4280172
You should set the schema_search_path in your config/database.yml
e.g.
development:
adapter: postgresql
encoding: unicode
database: test
pool: 5
username: user
password: password
#host: localhost
#port: 5432
# Schema search path. The server defaults to $user,public
schema_search_path: example

cron with whenever gem not running

I am using thinking sphinx for search.It searches properlt but the problem was whenever i add new records i have to do rake thinking_sphinx:index manually.So to run it automatically i am using whenever gem with cron but still its not happening automatically,Either i have to do
rake thinking_sphinx:index
or
whenever -w to create index automatically
Following is the code of config/schedule.rb :
every 10.minutes do
rake "thinking_sphinx:index", :environment => :development
end
every :reboot do
rake "thinking_sphinx:start",:environment => :development
end
when i do crontab -l i get following :
# Begin Whenever generated tasks for: store
#reboot /bin/bash -l -c 'cd /home/user/newsvn/alumnicell && RAILS_ENV=development bundle exec rake thinking_sphinx:start --silent'
0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /home/user/newsvn/alumnicell && RAILS_ENV=development bundle exec rake thinking_sphinx:index --silent'
# End Whenever generated tasks for: store
What and where is the problem that this not working?
Also i changed environment to development.by default it was production.what is the difference in these 2?
You can assign log file to track errors, then post it here.
Also I advise to set :environment globally.
Try this code:
set :environment, :development
set :output, 'tmp/whenever.log'
every 10.minutes do
rake "thinking_sphinx:index"
end
every :reboot do
rake "thinking_sphinx:start"
end
I will advise you to use thinking sphinx's delta indexes instead of using whenever cron jobs to rebuild thinking sphinx every 10 minutes.
As it's described here you should just add a boolean column delta to your model, set set_property :delta => true in your define_index block and run rake ts:rebuild
That's it.

How to add 'rake test' to project

When I ran rake -T, I discovered rake test was missing. What do I need to do to get this task in there? Specifically, I want to run rake test:benchmark but that doesn't seem to be loaded. For example...
$rake test:benchmark
rake aborted!
Don't know how to build task 'test:benchmark'
My config/application.rb file was missing this line:
require 'rails/all'
You only need:
require "rails/test_unit/railtie"
in your config/application.rb