Use Ruby closure_tree gem without rails - ruby-on-rails

I want to try the gem closure_tree to build a tree for my genealogy database.
I installed the gem under Ruby MRI 2.3.0 on a windows 7 box.
The versions of the dependencies are in the errorlog below.
$ gem install closure_tree
Fetching: with_advisory_lock-3.0.0.gem (100%)
Successfully installed with_advisory_lock-3.0.0
Fetching: closure_tree-6.2.0.gem (100%)
Successfully installed closure_tree-6.2.0
2 gems installed
When I try the most minimalistic code it fails, I suppose because I don't use Rails and don't do the migration which again I presume creates the table tag_hierachies
My question is: Can I use this gem without Rails and if so how ?
require 'active_record'
require 'closure_tree'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "sample.db"
)
if !ActiveRecord::Base.connection.table_exists?('tags')
ActiveRecord::Schema.define do
create_table :tags do |table|
table.column :name, :string
table.column :parent_id, :integer
end
end
end
class Tag < ActiveRecord::Base
has_closure_tree
end
grandparent = Tag.create(name: 'Grandparent')
gives
d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:513:in `table_structure': Could not find table 'tag_hierarchies' (ActiveRecord::StatementInvalid)
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:387:in `columns'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attributes.rb:93:in `columns'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attributes.rb:98:in `columns_hash'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:205:in `subclass_from_attributes?'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:54:in `new'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:50:in `create!'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/hierarchy_maintenance.rb:65:in `block in rebuild!'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/support.rb:108:in `block (2 levels) in with_advisory_lock'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/support.rb:108:in `block in with_advisory_lock'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/base.rb:77:in `yield_with_lock'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/base.rb:65:in `yield_with_lock_and_timeout'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/base.rb:48:in `with_advisory_lock_if_needed'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/concern.rb:16:in `with_advisory_lock_result'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/concern.rb:10:in `with_advisory_lock'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/support.rb:107:in `with_advisory_lock'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/hierarchy_maintenance.rb:63:in `rebuild!'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/hierarchy_maintenance.rb:39:in `_ct_after_save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:432:in `block in make_lambda'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `block in call'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `each'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `call'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_save_callbacks'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/callbacks.rb:302:in `create_or_update'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:120:in `save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/validations.rb:37:in `save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/dirty.rb:21:in `save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:286:in `block (2 levels) in save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:286:in `block in save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:285:in `save'
from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:34:in `create'
from C:/Users/Gebruiker/BoxSync/ruby_werk/acts_as_tree/closure_tree.rb:22:in `<main>'

Seems the installation instructions in README is focused with only Rails in mind. It does however tell you that you need another table(in addition to your current tags table) with the name tag_hierarchies.
In Rails, generation of this table seems to be part of the installation steps; Step 5 in the linked documentation.
So, you should be creating tag_hierarchies table with the following content, which is what rails g closure_tree:migration tag generates as far as the table structure is concerned. See create_hierarchies_table.rb.erb, as you may also want to add index as shown in this linked file.
So, updating your script to following worked as expected for me:
require 'active_record'
require 'closure_tree'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "sample.db"
)
if !ActiveRecord::Base.connection.data_source_exists?('tags')
ActiveRecord::Schema.define do
create_table :tags do |table|
table.column :name, :string
table.column :parent_id, :integer
end
end
end
# You also need a corresponding model's table name followed by "_hierarchies" table.
#
# Migration created based on
# `https://github.com/mceachen/closure_tree/blob/master/lib/generators/closure_tree/templates/create_hierarchies_table.rb.erb`
if !ActiveRecord::Base.connection.data_source_exists?(:tag_hierarchies)
ActiveRecord::Schema.define do
create_table :tag_hierarchies do |table|
table.integer :ancestor_id, null: false
table.integer :descendant_id, null: false
table.integer :generations, null: false
end
end
end
class Tag < ActiveRecord::Base
has_closure_tree
end
grandparent = Tag.create(name: 'Grandparent')
grandparent.children.create(name: 'Father')
grandparent.children.create(name: 'Uncle')

Related

Getting Rspec error no implicit conversion of Symbol into Integer with Mongoid

I'm tying to test my Rails app with Rspec, but I'm getting a no implicit conversion of Symbol into Integer error without any apparent reason. Based on the traceback I get I think the problem is related to Mongo/Mongoid, however, I can't figure out what it is exactly. The code runs perfectly in production. The error happens only when testing.
Brief look at the model without the other methods:
class Card
include Mongoid::Document
field :front, type: String
field :back, type: String
field :level, type: Integer, default: 1
field :review_date, type: DateTime, default: DateTime.now
has_many :card_statistic, dependent: :destroy
belongs_to :topic
belongs_to :user
validates :front, :back, :level, presence: true
validates :topic, presence: { is: true, message: "must belong to a topic." }
validates :user, presence: { is: true, message: "must belong to a user." }
validates :level, numericality: { only_integer: true, greater_than: 0 }
end
One function in the model that triggers the error:
def self.reset(card)
card.update(level: 1)
end
The test code:
it "puts the given card in level 1" do
card = create(:card)
Card.correct card
card.reload
Card.correct card
card.reload
expect(card.level).to eq(3)
card.reset
card.reload
expect(card.level).to eq(1)
end
Then, the traceback of the error I get:
1) Card puts the given card in level 1
Failure/Error: Card.reset card
TypeError:
no implicit conversion of Symbol into Integer
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/query_cache.rb:207:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `block (2 levels) in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:562:in `with_sorting'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:229:in `block in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:474:in `try_cache'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:228:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual.rb:20:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/builders/referenced/in.rb:20:in `build'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:43:in `create_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:26:in `__build__'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:99:in `get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:187:in `block in getter'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:395:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:334:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:97:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:371:in `invalid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:114:in `prepare_update'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:139:in `update_document'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/savable.rb:25:in `save'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:52:in `update'
# ./app/models/card.rb:57:in `reset'
# ./spec/models/card_spec.rb:32:in `block (2 levels) in <top (required)>'
The error is also triggered when testing the controllers. Even doing a get :index throws the error. Thanks in advance for your help.
Apparently, there where some issues with the very last builds of Mongoid and I was pulling the code directly from the repo without specifying a stable version. So the solution was to either downgrade the version to 4.0 or use the 5.0 beta build explicitly.
Either
gem 'mongoid', '~> 5.0.0.beta', github: 'mongoid/mongoid'
or
gem 'mongoid', '~> 4'
instead of
gem 'mongoid', '~> 5', github: 'mongoid/mongoid'

Rails 4.1: Can't cast Hash to string

Created a simple rake task to find Bikes for Sale in the USA. It works when i want to extract heading details but when I add other parameters like location, price , etc and save it to the Posts db I get error Can't cast Hash to string
Neils-MacBook-Pro-2:bike_scraper neilpatel$ rake scraper:scrape
rake aborted!
TypeError: can't cast Hash to string
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in `exec_insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/relation.rb:64:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:503:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:87:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `block in _create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:483:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:103:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/validations.rb:51:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block (2 levels) in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:208:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:267:in `save'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:53:in `block (3 levels) in <top (required)>'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `each'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `block (2 levels) in <top (required)>'
Tasks: TOP => scraper:scrape
(See full trace by running task with --trace)
scraper.rake
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri' #ruby gem
require 'JSON'
# Set API token and URL
auth_token = "fadddddddd4a23a1cc86e0d"
polling_url = "http://polling.3taps.com/poll"
# Specify request parameters
params = {
auth_token: auth_token,
anchor: 2109915749,
source: "CRAIG",
category_group: "SSSS",
category: "SBIK",
'location.country' => "USA",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request -hitting enter
result = JSON.parse(open(uri).read)
# Display results to screen (put string - display information)
#puts result["postings"].second["location"]
#store results in database
# Create new Post
result["postings"].each do |posting|
# Create new Post
#post = Post.new
#post.heading = posting["heading"]
#post.body = posting["body"]
#post.price = posting["price"]
#post.state = posting["location"]
#post.external_url = posting["external_url"]
#post.timestamp = posting["timestamp"]
# Save Post
#post.save
end
end
desc "TODO"
task destroy_all_posts: :environment do
end
end
Posts.db
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :heading
t.text :body
t.decimal :price
t.string :state
t.string :external_url
t.string :timestamp
t.timestamps
end
end
end
The error is coming from your #post.save line, so one of those fields that came back in posting is a Hash, not a String as you expect. It won't be price, so just check all the others by putsing them.
Update
I just ran your code, it's posting["location"] that's a Hash
Further Update
Looks like you just want the state out of that hash, ie.
#post.state = posting["location"]["state"]

Has and belongs to many association search in thinking sphinix rails 3.2.14

I am using Rails 3.2.14, thinking-sphinx 3.1.1 and Sphinx 2.2.4-id64-release (r4806)
I have two modals below:-
Album.rb
has_and_belongs_to_many :genres
Genre.rb
has_and_belongs_to_many :albums
So the associated table is albums_genres
I want to search albums using the genre_id
Example: When I pass a genre_id then it should return all the albums associated to that genre_id on the basis of third table albums_genres
I have tried like that
ThinkingSphinx::Index.define :album, :with => :active_record do
#other indexes
has genres.id, :as => :genre_ids, :source => :query
end
But when i run the rake task as rake ts:index i am getting the error as:-
rake aborted!
undefined method `join_table' for #<ActiveRecord::Reflection::AssociationReflection:0x000000079224d8>
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:15:in `quoted_foreign_key'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:28:in `to_sql'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:76:in `queries'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:5:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:15:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:45:in `query'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:38:in `multi_declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:23:in `declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:96:in `block in append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `each'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:132:in `prepare_for_render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:65:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:53:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:90:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `block in render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:24:in `index'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `eval'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `<main>'
It looks like this is a bug with Rails 3.2, :source => :query and HABTM joins (which is sadly not surprising - getting the code to work even with Rails 4.0 was painful and fragile).
I'm afraid your options are either upgrade to Rails 4 or not use the :source => :query option (and I know that means you lose some nice speed gains for indexing).
Odds of me getting a patch sorted in the near future is small (but others are welcome to give it a shot, if they wish).

TypeError: can't cast Array to string

I'm getting this error when trying to run a rake task using Nokogiri.
This is the code.
namespace :tops do
desc "Get Tops Description"
task get_description: :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://ceratoboutique.com/collections/tops"))
price = Array.new
items = Array.new
brands = Array.new
sale_price = Array.new
image = Array.new
item_url = Array.new
price = doc.xpath("//del").collect {|node| node.text.strip}
items = doc.xpath("//div/a/h4").collect {|node| node.text.strip}
brands = doc.xpath("//span[contains(#class,'vendor')]").collect {|node| node.text.strip}
sale_price = doc.xpath("//span[contains(#class, 'price')]/text()").collect {|node| node.text.strip}
image = doc.xpath("/div/a/img/#src").collect {|node| node.text.strip}
item_url = doc.css('div.details a').map { |link| link['href'] }.collect
price.each do |prices|
Tops.create(description: price)
end
end
end
This is the full error.
Rake aborted! TypeError: can't cast Array to string
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/quoting.rb:76:in type_cast'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:intype_cast'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in block in exec_query'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:inmap'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in exec_query'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in
exec_insert'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in
insert'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in
insert'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/relation.rb:64:in
insert'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:502:in
_create_record'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:87:in
_create_record'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in
block in _create_record'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in
run_callbacks'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in
_create_record'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/timestamp.rb:57:in
_create_record'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:482:in
create_or_update'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:in
block in create_or_update'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in
run_callbacks'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:in
create_or_update'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:103:in
save'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/validations.rb:51:in
save'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:21:in
save'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:in block (2 levels) in save'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:329:inblock in with_transaction_returning_status'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in
block in transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in
within_new_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in
transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:208:intransaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:326:in with_transaction_returning_status'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:inblock in save'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:283:in rollback_active_record_state!'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:267:insave'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:34:in
create' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:24:in
block (3 levels) in '
/Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:in each'
/Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:inblock (2
levels) in '
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in
call'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in
block in execute'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in
each'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in
execute'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:179:in
block in invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in
mon_synchronize'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:172:in
invoke_with_call_chain'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:165:in
invoke'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:150:in
invoke_task'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
block (2 levels) in top_level'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
each'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
block in top_level'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:in
run_with_threads'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in
top_level'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:in
block in run'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in
standard_exception_handling'
/Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:75:in
run' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/bin/rake:33:in' /usr/bin/rake:23:in load' /usr/bin/rake:23:in'
Edit: TOPS TABLE SCHEMA
ActiveRecord::Schema.define(version: 20140824150641) do
create_table "tops", force: true do |t|
t.string "brand"
t.string "description"
t.string "image"
t.decimal "price"
t.datetime "created_at"
t.datetime "updated_at"
t.string "url"
end
end
Seems that you have description field in table tops as 'varchar' or string, however you are trying to store array in it.
You need to change datatype of description field to text and then serialize the value before trying to store it.
Generate new migration file and run it, On your terminal:
rails g migration change_desc_in_my_tops
As you are using Rails 4, add following code in your migration file:
class ChangeDescInTops < ActiveRecord::Migration
def up
change_column :tops, :description, :text
end
def down
change_column :tops, :description, :string
end
end
Now run migration:
rake db:migrate
Update your tops model as:
class Tops < ActiveModel::Base
serialize :description, Array
...
...
end
Now try to run your rake task. Hope, it will help you.
NOTE: You should use singular model names. Your Tops model could be Top. It avoids confusion while using associations and it follows convention.
You quite confusing have set price to be an array (rather than calling it prices) which was possibly a contributing factor in your typo below
price.each do |prices|
Tops.create(description: price)
end
You are assigning the array you are iterating over, rather than the individual element that is being yielded.

Rails giving problems with relational table, SQLite3::SQLException: no such column

Can't seem to find a solution to this one. I have a relational table, BlabsUser, from which I'm trying to find and destroy a record. I'm doing this:
BlabsUser.find_by_user_id_and_blab_id(1,29).destroy
And getting this lovely error:
BlabsUser Load (0.2ms) SELECT "blabs_users".* FROM "blabs_users" WHERE "blabs_users"."user_id" = 1 AND "blabs_users"."blab_id" = 29 LIMIT 1
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:234:in `block in exec_query'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:223:in `exec_query'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:249:in `exec_delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:101:in `delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/relation.rb:351:in `delete_all'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/persistence.rb:94:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/locking/optimistic.rb:119:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `block in destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `block in destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `destroy'
from (irb):1
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Why is it looking for a column called blabs_users? Why isn't it just looking for a record in my blabs_users table? Also, more importantly, how do I fix this?
Edit:
And I've also tried this with the same problem:
BlabsUser.destroy_all(:blab_id => 29, :user_id => 1)
Edit 2, as per request:
My model is just
class BlabsUser < ActiveRecord::Base
end
Migration is just
class BlabsUsers < ActiveRecord::Migration
def self.up
create_table :blabs_users, :id => false, :force => true do |t|
t.integer :user_id
t.integer :blab_id
t.timestamps
end
end
def self.down
drop_table :blabs_users
end
end
ActiveRecord depends pretty heavily on having a unique key to work with (usually the primary key of the table) Since you explicitly don't have one defined (:id => false), I strongly suspect that your problem is related to that.
destroy() is also documented as Destroy an object (or multiple objects) that has the given id which strongly suggests it needs that unique id to work with.

Resources