I was trying to implement the textacular gem in my Rails 4 App. Basic and Advanced Search work fine, but when I try to implement Fuzzy Search, I encounter some trouble.
Installation Instructions from the Repository:
Finally, the #fuzzy_search method lets you use Postgres's trigram
search funcionality.
In order to use this, you'll need to make sure your database has the
pg_trgm module installed. On your development machine, you can require
textacular/tasks and run
rake textacular:install_trigram
Depending on your production environment, you might be able to use the
rake task, or you might have to manually run a command. For Postgres
9.1 and above, you'll want to run
CREATE EXTENSION pg_trgm;
What I've Tried
I tried running rake textacular:install_trigram and bundle exec rake textacular:install_trigram in the root directory of my app and I get this message:
/usr/local/Cellar/postgresql/9.2.4/share/postgresql/extension/pg_trgm.control
rake aborted!
PG::InsufficientPrivilege: ERROR: permission denied to create extension "pg_trgm"
HINT: Must be superuser to create this extension.
: CREATE EXTENSION pg_trgm;
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `exec'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/textacular-3.1.0/lib/textacular/postgres_module_installer.rb:54:in `install_postgres_91_module'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/textacular-3.1.0/lib/textacular/postgres_module_installer.rb:7:in `install_module'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/textacular-3.1.0/lib/textacular/tasks.rb:14:in `block (2 levels) in <top (required)>'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => textacular:install_trigram
Based on this, I tried sudo rake textacular:install_trigram and I got this:
cannot load such file -- bundler/setup
/Users/ssapra/Sameet/Programming/WebDev/Rails/myapp/config/boot.rb:4:in `<top (required)>'
/Users/ssapra/Sameet/Programming/WebDev/Rails/myapp/config/application.rb:1:in `<top (required)>'
/Users/ssapra/Sameet/Programming/WebDev/Rails/myapp/Rakefile:4:in `<top (required)>'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
(See full trace by running task with --trace)
Lastly, after reading the For Postgres 9.1 and above, I tried running psql and then CREATE EXTENSION pg_trgm;. No errors (although anytime I do it now, it will tell me it already exists). So I \quit and then replace my line of code that does work:
#search = Content.advanced_search(params[:search].split(" ").join("|")).basic_search(params[:search])
with:
#search = Content.fuzzy_search(title: params[:search]).fuzzy_search(text: params[:search])
or even:
#search = Content.fuzzy_search(params[:search])
and I get this:
PG::UndefinedFunction: ERROR: operator does not exist: character varying % unknown
LINE 1: ...NT(*) FROM "contents" WHERE (("contents"."title" % 'inv')) ...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "contents" WHERE (("contents"."title" % 'inv')) AND (("contents"."text" % 'inv'))
It gets stuck on the first line:
-if #contents.empty?
%p="No results found for: #{#query}"
-elsif !#query.empty?
%p="Your search results for: #{#query}"
Contents Controller
In my contents_controller.rb:
I have these three additions:
require 'textacular'
require 'textacular/tasks'
ActiveRecord::Base.extend(Textacular)
I am running Postgres 9.2.4. Any ideas?
Related
I'm very new to Ruby on Rails. I created a model by running the rails g scaffold command, and then tried to run rails db:migrate in the wsl terminal which gave me this huge error:
rails db:migrate
/home/nidhi/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/railties-6.1.4.1/lib/rails/app_loader.rb:53: warning: Insecure world writable dir /mnt/c in PATH, mode 040777
Rails Error: Unable to access log file. Please ensure that /mnt/c/Windows/system32/railsfriends/friends/log/development.log exists and is writable (ie, make it writable for user and group: chmod 0664 /mnt/c/Windows/system32/railsfriends/friends/log/development.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
(0.6ms) SELECT sqlite_version(*)
(0.3ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY)
rails aborted!
ActiveRecord::StatementInvalid: SQLite3::ReadOnlyException: attempt to write a readonly database
/mnt/c/Windows/system32/railsfriends/friends/bin/rails:5:in `<top (required)>'
/mnt/c/Windows/system32/railsfriends/friends/bin/spring:10:in `block in <top (required)>'
/mnt/c/Windows/system32/railsfriends/friends/bin/spring:7:in `tap'
/mnt/c/Windows/system32/railsfriends/friends/bin/spring:7:in `<top (required)>'
Caused by:
SQLite3::ReadOnlyException: attempt to write a readonly database
/mnt/c/Windows/system32/railsfriends/friends/bin/rails:5:in `<top (required)>'
/mnt/c/Windows/system32/railsfriends/friends/bin/spring:10:in `block in <top (required)>'
/mnt/c/Windows/system32/railsfriends/friends/bin/spring:7:in `tap'
/mnt/c/Windows/system32/railsfriends/friends/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I tried unchecking the "Read only" property of the development.log file it mentions, but that did not help. How can I solve this error?
Restarting my computer worked for me! I just had to go to my original folder and uncheck the "Read only" property, then restart the computer. The rails db:migrate command works now.
I have an pre-existing database named exam_prep in postgresql, which i am using in flask.
I have created a new Rails application and i want to use that database with it.
So far I have run rake db:structure:dump, which created a structure.sql under the db folder.
After that I ran db:structure:load, but got the following error:
psql:/Users/namangupta/Desktop/app/db/structure.sql:49: ERROR: type "batch_types_enum" already exists
rake aborted!
failed to execute:
psql -v ON_ERROR_STOP=1 -q -f /Users/namangupta/Desktop/app/db/structure.sql exam_prep
Please check the output above for any errors and make sure that `psql` is installed in your PATH and has proper permissions.
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:108:in `run_cmd'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:80:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:223:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:236:in `load_schema'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:255:in `block in load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:254:in `load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:290:in `block (3 levels) in <top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `eval'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:load
(See full trace by running task with --trace)
I want Rails to use the database and make a models within this. Any ideas?
The db:structure:load rake task creates a tables and columns from the structure.sql or schema.rb. You shouldn't need to run it, as your existing database already has all the tables you need.
Your next step would be to go about creating your model files and pointing them at the appropriate tables.
I'm on Example 46 of Learn Ruby The Hard Way - Creating a Project Skeleton
Link: http://learnrubythehardway.org/book/ex46.html
So I created all the directories which show up as the following:
skeleton/
NAME.gemspec
Rakefile
data
ext/
tests/
bin/
NAME
doc/
lib/
NAME
NAME.rb
lib/NAME
tests/
test_NAME.rb
I'm in the Skeleton directory in Powershell.
When I run rake test as the tutorial says to I get an error.
Here's the result from Powershell:
PS C:\ruby\learn\projects\skeleton> rake test
C:/RailsInstaller/Ruby2.1.0/bin/ruby.exe -I"lib;tests" -I"C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4
.2/lib" "C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "tests/test_NAME
.rb"
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:1:in `<top (required)>'
C:/ruby/learn/projects/skeleton/tests/test_NAME.rb:2:in `<top (required)>'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:15:in `block in <main>'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `select'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `<main>'
MiniTest::Unit::TestCase is now Minitest::Test. From C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit/testcase.rb:8:
in `<module:Unit>'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:676:in `<class:Runner>': undefined method `_run_suite' for class
`Test::Unit::Runner' (NameError)
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:261:in `<module:Unit>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:15:in `<module:Test>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/test/unit.rb:7:in `<top (required)>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/ruby/learn/projects/skeleton/tests/test_NAME.rb:2:in `<top (required)>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:15:in `block
in <main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `select'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb:4:in `<main>'
rake aborted!
Command failed with status (1): [ruby -I"lib;tests" -I"C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/
lib" "C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/rake_test_loader.rb" "tests/test_NAME.rb
" ]
Tasks: TOP => test
(See full trace by running task with --trace)
The book says I should get the following response:
Loaded suite tests/test_NAME
Started
.
Finished in 0.000226 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
But instead I get an error. I've search for fixes and using gem install minitest but keep in mind I don't have a gemfile and I don't know if I should for this type of project. If I needed one I assume the tutorial would say so. So what do I do to fix this issue?
You don't need a Gemfile. While you are at skeleton directory, just do:
gem install 'test-unit'
and then from the same directory:
rake test
All tests will pass.
P.S. I just reproduced the whole thing locally following your tutorial and got the error message as yours. Then, I installed the test-unit gem and after that the rake test is working without any error.
So I am trying to do bundle install . But it is throwing the below errors. What is the problem here?
bundle exec rake db:create:all
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:66)
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:67)
rake aborted!
Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Tasks: TOP => db:create:all => db:load_config
(See full trace by running task with --trace)
Earlier I had tried the below:
rake db:create:all
rake aborted!
Gem::LoadError: You have already activated rake 10.4.2, but your Gemfile requires rake 10.3.2. Prepending `bundle exec` to your command may solve this.
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler/runtime.rb:34:in `block in setup'
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler/runtime.rb:19:in `setup'
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler.rb:122:in `setup'
/home/.gem/ruby/2.1.3/gems/bundler-1.7.12/lib/bundler/setup.rb:7:in `<top (required)>'
/home/project/config/boot.rb:4:in `<top (required)>'
/home/project/config/application.rb:1:in `<top (required)>'
/home/project/Rakefile:4:in `<top (required)>'
LoadError: cannot load such file -- bundler/setup
/home/project/config/boot.rb:4:in `<top (required)>'
/home/project/config/application.rb:1:in `<top (required)>'
/home/project/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
$ bundle exec rake db:create:all
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:66)
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:67)
rake aborted!
Cannot load `Rails.application.database_configuration`:
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
/home/.gem/ruby/2.1.3/gems/activerecord-4.1.5/lib/active_record/railtie.rb:43:in `block (3 levels) in <class:Railtie>'
Tasks: TOP => db:create:all => db:load_config
(See full trace by running task with --trace)
You are trying to run "rake db:create:all" which would create your database but as error states - it seems you don't have your config/database.yml file. It's a configuration file that tells rails how to connect to your database. You can read more about it here.
If you really want to run bundle install, just use "bundle install" command instead. This will install all gems listed in your Gemfile.
If you will get problems with rake version conflicts, check out this stack overflow question.
It seems you have no config/database.yml file in your project's directory.
Why I think so? Follow me ...
Could not load database configuration. No such file -
/home/.gem/ruby/2.1.3/gems/railties-4.1.5/lib/rails/application/configuration.rb:105:in `database_configuration'
Ok, let's take a look at this code where we have error:
yaml = Pathname.new(paths["config/database"].existent.first || "")
config = if yaml.exist?
require "yaml"
require "erb"
YAML.load(ERB.new(yaml.read).result) || {}
elsif ENV['DATABASE_URL']
# Value from ENV['DATABASE_URL'] is set to default database connection
# by Active Record.
{}
else
raise "Could not load database configuration. No such file - #{yaml}"
end
As you can see - if no such file or ENV variable then rails throws error.
I am getting errors when I run: rake cucumber:ok
Can you please tell me how to fix the following error?
Gem versions I am using:
rails Rails 3.0.8.rc4
rspec 2.6.3
cucumber 0.10.3
Errors when I run: rake cucumber:ok
[ticketee (master)]$ rake cucumber:ok
/Users/duser/.rvm/rubies/ruby-1.9.2-p180/bin/ruby -S bundle exec cucumber --profile default
Using the default profile...
features/creating_projects.feature: Lexing error on line 12: '%%_FEATURE_END_%%'. See http://wiki.github.com/cucumber/gherkin/lexingerror for more information. (Gherkin::Lexer::LexingError)
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/gherkin-2.3.10/lib/gherkin/lexer/i18n_lexer.rb:23:in `scan'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/gherkin-2.3.10/lib/gherkin/lexer/i18n_lexer.rb:23:in `scan'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/gherkin-2.3.10/lib/gherkin/parser/parser.rb:31:in `parse'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/feature_file.rb:37:in `parse'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/runtime/features_loader.rb:28:in `block in load'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/runtime/features_loader.rb:26:in `each'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/runtime/features_loader.rb:26:in `load'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/runtime/features_loader.rb:14:in `features'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/runtime.rb:132:in `features'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/runtime.rb:45:in `run!'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/cli/main.rb:43:in `execute!'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/lib/cucumber/cli/main.rb:20:in `execute'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/gems/cucumber-0.10.3/bin/cucumber:14:in `<top (required)>'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/bin/cucumber:19:in `load'
/Users/duser/.rvm/gems/ruby-1.9.2-p180/bin/cucumber:19:in `<main>'
rake aborted!
Command failed with status (1): [/Users/duser/.rvm/rubies/ruby-1.9.2-p18...]
Tasks: TOP => cucumber:ok
(See full trace by running task with --trace)
[ticketee (master)]$
Contents of features/creating_projects.feature
Feature: Creating projects
In order to have projects to assign tickets to
As a user I want to create them easily
Scenarios: Creating a project
Given I am on the homepage
When I follow "New Project"
And I fill in "Name" with "Text Mate 2"
And I press "Create Project"
Then I should see "Project has been created"
I figured out the problem:
In the features/creating_projects.feature file, I had "Scenarios:", and it should be "Scenario:". This is the reason for the errors.
You have a lexing error on line 12 of features/creating_projects.feature and its most likely at the end of the feature definition. Are you using the correct syntax in the file?