How to migrate correctly with sequel on Access 2007 - ruby

When I migrate my application to Access 2007 with sequel library of Ruby. I get the errors as follows. Does anybody know how to migrate correctly? Thanks.
C:\ContractManagement>rackup
C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/adapters/ado.rb:63:
in `method_missing': WIN32OLERuntimeError: (in OLE method `Execute': ) (Sequel::
DatabaseError)
OLE error code:80040E14 in Microsoft Office Access Database Engine
Syntax error (missing operator) in query expression 'LIMIT 1 1'.
HRESULT error code:0x80020009
Exception occurred.
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/adapte
rs/ado.rb:63:in `block (2 levels) in execute'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/databa
se/logging.rb:28:in `log_yield'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/adapte
rs/ado.rb:63:in `block in execute'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/connec
tion_pool/threaded.rb:84:in `hold'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/databa
se/connecting.rb:226:in `synchronize'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/adapte
rs/ado.rb:61:in `execute'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/datase
t/actions.rb:541:in `execute'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/adapte
rs/ado.rb:97:in `fetch_rows'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/datase
t/actions.rb:123:in `each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/datase
t/actions.rb:449:in `single_record'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/datase
t/actions.rb:457:in `single_value'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/datase
t/actions.rb:200:in `get'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/datase
t/actions.rb:133:in `empty?'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/extens
ions/migration.rb:499:in `schema_dataset'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/extens
ions/migration.rb:381:in `initialize'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/extens
ions/migration.rb:422:in `initialize'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/extens
ions/migration.rb:332:in `new'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/extens
ions/migration.rb:332:in `run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sequel-3.23.0/lib/sequel/extens
ions/migration.rb:316:in `apply'
from C:/ContractManagement/config.ru:12:in `block in <main>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/builder.rb:
51:in `instance_eval'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/builder.rb:
51:in `initialize'
from C:/ContractManagement/config.ru:1:in `new'
from C:/ContractManagement/config.ru:1:in `<main>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/builder.rb:
40:in `eval'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/builder.rb:
40:in `parse_file'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/server.rb:2
00:in `app'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/server.rb:3
01:in `wrapped_app'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/server.rb:2
52:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/lib/rack/server.rb:1
37:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.3.0/bin/rackup:4:in `<to
p (required)>'
from C:/Ruby192/bin/rackup:19:in `load'
from C:/Ruby192/bin/rackup:19:in `<main>'
Here is the connection configuration.
DB = Sequel.ado(:conn_string=>'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ContractManagement1.accdb')
And here is the migration steps. All are passed on PostgreSQL 8.4.
Sequel.migration do
up do
create_table :people do
primary_key :id
String :name, :size => 20, :unique => true, :null => false
String :password, :size => 30, :null => false
String :role, :size => 20, :null => false
end
end
down do
drop_table :people
end
end
Sequel.migration do
up do
create_table :currencies do
primary_key :id
String :name, :size => 10, :null => false
end
end
down do
drop_table :currencies
end
end
Sequel.migration do
up do
create_table :statuses do
primary_key :id
String :name, :size => 10, :null => false
end
end
down do
drop_table :statuses
end
end
# encoding: utf-8
Sequel.migration do
up do
create_table :contracts do
primary_key :id
String :contract_id, :size => 36, :unique => true, :null => false
String :content, :size => 150
String :supplier, :size => 30
Date :created_on
Date :expired_on
BigDecimal :amount, :size => [10, 2], :null => false
Fixnum :debt_days
Fixnum :guarantee_period
String :order_id, :size => 50 # 订单号
String :supplier_contract_id, :size => 36
String :operator, :size => 30
foreign_key :currency_id, :currencies, :on_delete => :cascade, :on_update => :cascade, :null => false
foreign_key :status_id, :statuses, :on_delete => :cascade, :on_update => :cascade, :null => false
constraint(:min_amount) { amount >= 0.01 }
end
end
down do
drop_table :contracts
end
end
# encoding: utf-8
Sequel.migration do
up do
self[:currencies].insert(:name => "人民币")
self[:currencies].insert(:name => "欧元")
self[:currencies].insert(:name => "美元")
end
down do
self[:currencies].delete
end
end
# encoding: utf-8
Sequel.migration do
up do
self[:statuses].insert(:name => "执行中")
self[:statuses].insert(:name => "关闭")
self[:statuses].insert(:name => "作废")
end
down do
self[:statuses].delete
end
end
# encoding: utf-8
Sequel.migration do
up do
create_table :payments do
primary_key :id
BigDecimal :prepayment, :size => [10, 2], :default => 0 # 预付金额(元)
BigDecimal :offset_prepayment, :size => [10, 2], :default => 0 # 冲预付
BigDecimal :guarantee_price, :size => [10, 2], :default => 0 # 质保金
BigDecimal :request_amount, :size => [10, 2], :default => 0 # 申请付款额
foreign_key :contract_id, :contracts, :on_delete => :cascade, :on_update => :cascade
foreign_key :person_id, :people, :on_delete => :cascade, :on_update => :cascade
constraint(:offset_prepayment_is_not_greater_than_prepayment) { prepayment >= offset_prepayment } # offset_prepayment不能大于prepayment
end
end
down do
drop_table :payments
end
end
Sequel.migration do
up do
{"admin" => "Admin", "contract" => "ContractOperator", "payment" => "PaymentOperator", "report" => "ReportReviewer"}.each do |n, r|
self[:people].insert(:name => n, :password => n, :role => r)
end
self[:people].insert(:name => "payment1", :password => "payment1", :role => "PaymentOperator")
end
down do
self[:people].delete
end
end

I think you get this error message ...
OLE error code:80040E14 in Microsoft Office Access Database Engine
Syntax error (missing operator) in query expression 'LIMIT 1 1'.
... because Access SQL doesn't support LIMIT. See this reply to a related Stack Overflow question.
See whether you can use Access SQL TOP N as a substitute for LIMIT N.
SELECT TOP 1 m.id, m.paid_in_full, m.date_field
FROM MyTable AS m
ORDER BY m.date_field DESC;

This is fixed in the master branch of Sequel, version 3.24.0 (which will be released next week) will have the bugfix.

Related

Error message when running Rake command in Rails app

I'm trying to create posts in my Rails app by pulling data from a CSV file.
When I try to run a Rake command, I get the error message below. What's wrong with this code?
SEED.RAKE FILE
require 'csv'
namespace :csv do
desc "Import CSV Data for Michelin Star Restaurants"
task :post => :environment do
csv_file_path = 'db/data.csv'
CSV.foreach(csv_file_path) do |row|
Post.create({
:name => row[1],
:address => row[2],
:city => row[3],
:michelin_status => row[4],
:website => row[5],
:phone => row[6],
:longitude => row[7],
:latitude => row[8],
:inthenews => row[9],
:google_reviews => row[10],
})
end
end
end
ERROR MESSAGE FROM CONSOLE
Faisals-Air:dinner fkhalid2008$ rake csv:post
rake aborted!
ArgumentError: invalid byte sequence in UTF-8
/Users/fkhalid2008/dinner/lib/tasks/seed.rake:11:in `block (2 levels) in <top (required)>'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
/Users/fkhalid2008/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => csv:post
(See full trace by running task with --trace)
DB MIGRATE FILE
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :name
t.string :inthenews
t.string :michelin_status
t.string :google_reviews
t.string :address
t.string :city
t.string :website
t.integer :phone
t.integer :longitude
t.integer :latitude
t.timestamps null: false
end
end
end
CSV FILE SCREENSHOT
This is because somewhere in your file there are some invalid bytes.
To avoid this issue, you can use scrub method like this:
CSV.foreach(csv_file_path) do |row|
Post.create({
:name => row[1].scrub,
:address => row[2].scrub,
:city => row[3].scrub,
:michelin_status => row[4].scrub,
:website => row[5].scrub,
:phone => row[6].scrub,
:longitude => row[7].scrub,
:latitude => row[8].scrub,
:inthenews => row[9].scrub,
:google_reviews => row[10].scrub,
})
end
Update
Try to specify the encoding type when you read the CSV file like this:
CSV.foreach(csv_file_path, "r:ISO-8859-1") do |row|
. . .
. . .
end
You can simply convert the original csv file to utf8 with nkf.
$ nkf -w --overwrite db/data.csv

rake db:migrate returns with error

I ran rake db:migrate and I came across this error in terminal
SQLite3::SQLException: no such table: admin_users: ALTER TABLE "admin_users" ADD "username" varchar(25)/Users/IsaiahxD/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
/Users/IsaiahxD/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
/Users/IsaiahxD/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
/Users/IsaiahxD/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
/Users/IsaiahxD/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'
/Users/IsaiahxD/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
I looked into my alter_user.rb file class AlterUsers < ActiveRecord::Migration
this is the file 20150825065823_alter_users.rb
def up
rename_table("users", "admin_users.broken")
add_column("admin_users", "username", :string, :limit => 25, :after => "email" )
change_column("admin_users", "email", :string, :limit => 100)
rename_column("admin_users", "broken", "hashed_password")
puts "*** Adding an index is next ***"
add_index("admin_users", "username")
end
def down
remove_index("admin_users", "username")
rename_column("admin_users", "hashed_password", "password")
change_column("admin_users", "email", :string, :default => "" , :null => false)
remove_column("admin_users", "username")
rename_table("admin_users", "users")
end
In your #up method, you're adding columns to admin_users but you haven't defined that table yet.
Instead of:
rename_table("users", "admin_users.broken")
try:
rename_table("users", "admin_users")

rake ts:configure rake ts:index thinking-sphinx-3.1.1 ruby-2.0.0 error

I am trying to configure a rails running application on my localhost. I have not used sphinx before. I have installed the sphinx-search on my Ubuntu machine and its service is running.
When i try to execute rake ts:index or rake ts:configure I get the following error:
NoMethodError: undefined method `klass' for nil:NilClass
Here is the full stack trace:
rake ts:configure --trace
** Invoke ts:configure (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute ts:configure
Generating configuration to /home/baran/code/atdhe/config/development.sphinx.conf
rake aborted!
NoMethodError: undefined method `klass' for nil:NilClass
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib
/thinking_sphinx/active_record/attribute/type.rb:37:in `block in associations'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib
/thinking_sphinx/active_record/attribute/type.rb:35:in `collect'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib /thinking_sphinx/active_record/attribute/type.rb:35:in `associations'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/type.rb:62:in `multi_from_associations'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib /thinking_sphinx/active_record/attribute/type.rb:9:in `multi?'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute.rb:4:in `multi?'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:18:in `collection_type'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:96:in `block in append_presenter_to_attribute_array'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `each'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `append_presenter_to_attribute_array'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:132:in `prepare_for_render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:65:in `render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `block in render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `collect'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:53:in `render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `block in render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `collect'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:90:in `render'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `block in render_to_file'
/home/baran/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:36:in `open'
/home/baran/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:36:in `open'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `render_to_file'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/tasks.rb:4:in `block (2 levels) in <top (required)>'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:240:in `call'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:235:in `each'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/baran/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:150:in `invoke_task'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/lib/rake/application.rb:75:in `run'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/bin/rake:23:in `load'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/bin/rake:23:in `<main>'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/bin/ruby_executable_hooks:15:in `eval'
/home/baran/.rvm/gems/ruby-2.0.0-p481#athde/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => ts:configure
Here are the indices files:
ThinkingSphinx::Index.define :restaurant, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
indexes school(:school_name), :sortable => true, :as => :school_name
indexes school(:branch_name), :sortable => true, :as => :branch_name
indexes school(:delta) => ThinkingSphinx::Deltas::DelayedDelta
has restaurant_info(:is_pick_up), :as => :pick_up, :facet => true
has delivery_info(:is_delivery), :as => :delivery, :facet => true
indexes contact_info(:restaurant_name), :sortable => true
indexes delivery_info(:delivery_charges), :as => :delivery_charges
has school(:id), :as => :school_id, :facet => true
has restaurant_categories(:id), :as => :restaurant_category_ids, :facet => true
has restaurant_info(:min_order), :as => :min_order, :type => :float, :sortable => true
has avg_rating, :as => :rating, :type => :integer, :sortable => true
has ranking, :as => :ranking, :type => :integer, :sortable => true
has delivery_info(:delivery_estimated_time), :as => :delivery_eta, :type => :integer, :sortable => true
set_property :min_infix_len => 1
set_property :delta => ThinkingSphinx::Deltas::DelayedDelta
end
ThinkingSphinx::Index.define :school, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
indexes :school_name, :sortable => true, :as => :school_name
indexes buildings(:building_name), :sortable => true
set_property :min_infix_len => 1
set_property :delta => ThinkingSphinx::Deltas::DelayedDelta
end
Here's your indexes slightly cleaned up:
ThinkingSphinx::Index.define :restaurant, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
indexes school.school_name, :sortable => true, :as => :school_name
indexes school.branch_name, :sortable => true, :as => :branch_name
indexes contact_info.restaurant_name, :sortable => true, :as => :restaurant_name
indexes delivery_info.delivery_charges, :as => :delivery_charges
has restaurant_info.is_pick_up, :as => :pick_up, :facet => true
has delivery_info.is_delivery, :as => :delivery, :facet => true
has school.id, :as => :school_id, :facet => true
has restaurant_categories.id, :as => :restaurant_category_ids, :facet => true
has restaurant_info.min_order, :as => :min_order, :type => :float
has avg_rating, :as => :rating, :type => :integer
has ranking, :as => :ranking, :type => :integer
has delivery_info.delivery_estimated_time, :as => :delivery_eta, :type => :integer
set_property :min_infix_len => 1
end
ThinkingSphinx::Index.define :school, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
indexes school_name, :sortable => true, :as => :school_name
indexes buildings.building_name, :as => :building_name, :sortable => true
set_property :min_infix_len => 1
end
Changes I made:
:sortable => true removed from attributes (the has calls). Attributes are sortable by their very nature, so this option changes nothing.
Removed the set_property call for deltas - this is done at the top of the index since TS v3 (as you're doing already).
Removed a field that was referencing the delta column, which looked kind of like a delta setting.
Changed column calls as symbols/method arguments to be method names - what you were using was required with Ruby 1.8, but now is not needed (and may be buggy) with TS v3 and Ruby 1.9 or newer.
Made sure all fields and attributes referring to columns via associations had custom names (the :as option).
It's also worth noting that you shouldn't need to specify types for attributes if the columns being used are those types. So, if avg_rating is an integer, then you don't need the :type => :integer option for that attribute.
My guess it was the combination of field/delta setting that was the main cause of the problem, but give this a spin, see if that helps things.

Rails 3 with composed_of model and validation get error "NoMethodError: undefined method `marked_for_destruction?'"

I have tried to add custom validation as is written in Rails 3 with composed_of model and validation but i got strange error: "NoMethodError: undefined method `marked_for_destruction?' for Money"
And I don't understand what is wrong.
Can you help me?
Model:
# == Schema Information
#
# Table name: transactions
#
# id :integer not null, primary key
# text :string(255)
# amount_cents :integer default(0), not null
# ...
class Transaction < ActiveRecord::Base
attr_accessible :text, :amount, ...
validates :text, :length => { :maximum => 255 }
composed_of :amount, :class_name => "Money", :mapping => %w(amount_cents cents),
:converter => Proc.new { |value| Money.to_money(value) }
validates :amount, :presence => true, :numericality => { :greater_than_or_equal_to => 0 }
validates_associated :amount
...
end
class Money
attr_reader :cents
def initialize(cents)
#cents = cents
end
class << self
def to_money(str_money)
cents = (str_money.to_f * 100).to_i
Money.new(cents)
end
def to_money?(str_money)
/\A\d+(\.\d+)?\z/ == str_money.to_s
end
end
def ==(value)
#cents == self.class.to_money(value).cents
end
def to_i
#cents
end
def to_f
#cents.to_f
end
def to_s
return nil if #cents.nil?
unit, subunit = #cents.abs.divmod(100)
unit_str = ""
subunit_str = ""
fraction_str = ""
unit_str, subunit_str = unit.to_s, subunit.to_s
subunit_str.insert(0, '0') while subunit_str.length < 2
absolute_str = "#{unit_str}.#{subunit_str}#{fraction_str}"
absolute_str.tap do |str|
str.insert(0, "-") if #cents < 0
end
end
def inspect
"#<Money cents:#{#cents} to_s:#{self.to_s}>"
end
end
Error:
1.9.3p194 :001 > t = Transaction.new
=> #<Transaction id: nil, text: nil, amount_cents: 0, date: nil, created_at: nil, updated_at: nil, transaction_type_id: nil, account_id: nil, user_id: nil, trans_account_id: nil, trans_amount_cents: 0>
1.9.3p194 :002 > t.amount = 100
=> 100
1.9.3p194 :003 > t.valid?
NoMethodError: undefined method `marked_for_destruction?' for #<Money cents:10000 to_s:100.00>
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `block in validate_each'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `reject'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations/associated.rb:5:in `validate_each'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:153:in `block in validate'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:150:in `each'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validator.rb:150:in `validate'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:310:in `_callback_before_21'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:462:in `_run__74709952__validate__911291598__callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations.rb:227:in `run_validations!'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:403:in `_run__74709952__validation__911291598__callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activemodel-3.2.8/lib/active_model/validations.rb:194:in `valid?'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/activerecord-3.2.8/lib/active_record/validations.rb:69:in `valid?'
from (irb):3
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/alexvs/.rvm/gems/ruby-1.9.3-p194#rails3tutorial2ndEd/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

Sequel::Model: Where the methods like create_table come from?

I am trying to understand how Sequel works. The example below inherit from Sequel::Model and calls set_schema, create_table, etc.
I was trying to find the documentation for these methods, but no luck on the rdoc for Sequel::Model: http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html
Where are these methods coming from and how does Sequel::Model make them available?
class Task < Sequel::Model
set_schema do
primary_key :id
varchar :title, :unique => true, :empty => false
boolean :done, :default => false
end
create_table unless table_exists?
if empty?
create :title => 'Laundry'
create :title => 'Wash dishes'
end
end
They're defined in Sequel::Plugins::Schema::ClassMethods (lib/sequel/plugins/schema.rb) and included when you call plugin :schema in your model.
http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/Schema/ClassMethods.html#M000110
http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html#M000130
The example in the question is incomplete and won't work unless a connection to a database is setup and the plugin :schema is called from the model.
irb(main):001:0> require "rubygems"
=> true
irb(main):002:0> require "sequel"
=> true
irb(main):003:0>
irb(main):004:0* # connect to an in-memory database
irb(main):005:0* DB = Sequel.sqlite
=> #<Sequel::SQLite::Database: "sqlite:/">
irb(main):006:0> class Task < Sequel::Model
irb(main):007:1> set_schema do
irb(main):008:2* primary_key :id
irb(main):009:2>
irb(main):010:2* varchar :title, :unique => true, :empty => false
irb(main):011:2> boolean :done, :default => false
irb(main):012:2> end
irb(main):013:1>
irb(main):014:1* create_table unless table_exists?
irb(main):015:1>
irb(main):016:1* if empty?
irb(main):017:2> create :title => 'Laundry'
irb(main):018:2> create :title => 'Wash dishes'
irb(main):019:2> end
irb(main):020:1> end
NoMethodError: undefined method `set_schema' for Task:Class
from (irb):7
irb(main):021:0> class Task < Sequel::Model
irb(main):022:1> plugin :schema
irb(main):023:1> set_schema do
irb(main):024:2* primary_key :id
irb(main):025:2>
irb(main):026:2* varchar :title, :unique => true, :empty => false
irb(main):027:2> boolean :done, :default => false
irb(main):028:2> end
irb(main):029:1>
irb(main):030:1* create_table unless table_exists?
irb(main):031:1>
irb(main):032:1* if empty?
irb(main):033:2> create :title => 'Laundry'
irb(main):034:2> create :title => 'Wash dishes'
irb(main):035:2> end
irb(main):036:1> end
=> #<Task #values={:title=>"Wash dishes", :done=>false, :id=>2}>
irb(main):037:0>

Resources