SQL Exception on ruby On Rails platform - ruby-on-rails

There are two model classes in my project. The first is User:
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :micropost1s
end
The other is Micropost1
class Micropost1 < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
validates :content, :length => { :maximum => 140 }
end
I am not getting where I am wrong. First I run this command for assigning variable first_user:
first_user = User.first
After that I write in console:
first_user.micropost1s
I am getting this error log:
Micropost1 Load (0.3ms) SELECT "micropost1s".* FROM "micropost1s" WHERE "micropost1s"."user_id" = 1
SQLite3::SQLException: no such column: micropost1s.user_id: SELECT "micropost1s".* FROM "micropost1s" WHERE "micropost1s"."user_id" = 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: micropost1s.user_id: SELECT "micropost1s".* FROM "micropost1s" WHERE "micropost1s"."user_id" = 1
from /var/lib/gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
from /var/lib/gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
from /var/lib/gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb:246:in `exec_query'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log'
from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb:467:in `select'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/querying.rb:38:in `find_by_sql'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/explain.rb:40:in `logging_query_plan'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/querying.rb:37:in `find_by_sql'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/relation.rb:171:in `exec_queries'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/relation.rb:160:in `to_a'
... 5 levels...
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `__send__'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
from /usr/lib/ruby/1.8/irb.rb:310:in `output_value'
from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status'
from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:71:in `start'
from /usr/lib/ruby/1.8/irb.rb:70:in `catch'
from /usr/lib/ruby/1.8/irb.rb:70:in `start'
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:41
from script/rails:6:in `require'
from script/rails:6irb(main):008:0>

Sounds like you haven't run the migrations generated by creating your models:
$ rake db:migrate
If this doesn't work, you may need to add a user_id column to your Micropost1 model. E.g:
$ rails g migration add_user_id_to_micropost1s
Then edit the generated migration to create the field as desired. Alternatively you could re-create the model with a reference to a user:
$ rails g model Micropost1 something:string user:references

Related

Use Ruby closure_tree gem without 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')

First course table to add a field open, type Boolean;rake db:migrate Operation error?

class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean
change_column_default(:courses, :open, {:from=>true, :to=>false})
end
end
!!!!!then Operation!!!!!!
rake db:migrate
== 20161126133112 AddOpenAttributeToCourses: migrating ========================
-- add_column(:courses, :open, :boolean)
-> 0.1047s
-- change_column_default(:courses, :open, {:from=>true, :to=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
: ALTER TABLE "courses" ALTER COLUMN "open" SET DEFAULT '---
:from: true
:to: false
'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:455:in `change_column_default'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:665:in `block in method_missing'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `block in say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:654:in `method_missing'
/home/ubuntu/workspace/db/migrate/20161126133112_add_open_attribute_to_courses.rb:4:in `change'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:608:in `exec_migration'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:591:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:590:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:768:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:959:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:823:in `up'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:801:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
You're trying to use the Rails5 arguments to change_column_default:
change_column_default(table_name, column_name, default_or_changes)
[...]
Passing a hash containing :from and :to will make this change reversible in migration:
change_column_default(:posts, :state, from: nil, to: "draft")
with Rails4:
change_column_default(table_name, column_name, default)
Sets a new default value for a column: [...]
The funny looking stuff you see in the SQL:
---
:from: true
:to: false
is the YAML version of your { :from => true, :to => false } Ruby Hash BTW.
You need to use separate up and down methods and simpler version of change_column_default with Rails4:
def up
change_column_default(:courses, :open, false)
end
def down
change_column_default(:courses, :open, true)
end
or just set the default when you create the column:
class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean, :default => false
end
end
BTW, since you didn't specify a :default when you added the column it would have a default of null, not true.
You can create the column and set a default in one line:
add_column :courses, :open, :boolean, default: true
Not sure exactly why you're getting that error. Maybe it's because you say "change the default from false to true" but the default was not false, actually it was nil.

thinking sphinx has_many & has_many :through

i need to index those association:
Author has_many :contributions
Author has_many :books, through: :contributions
I tried to write my index like:
ThinkingSphinx::Index.define :author, :with => :active_record do
indexes books(:title), as: :book
end
But it has no intention of working. any ideas?
EDIT
When i tried this, or pretty much every other solution i got this error after rake ts:index :
Generating configuration to /Users/Kuba/Desktop/Rails/black/config/development.sphinx.conf
rake aborted!
undefined method `active_record' for #<ActiveRecord::Associations::JoinDependency:0x007faa3d1e5070>
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/associations.rb:78:in `reflection_for'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/associations.rb:50:in `join_for'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/associations.rb:38:in `model_for'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:42:in `column_exists?'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:48:in `column_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:55:in `block in columns_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:54:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:54:in `columns_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:31:in `casted_column_with_table'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/property_sql_presenter.rb:15:in `to_select'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_builder.rb:127:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_builder.rb:127:in `select_clause'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_builder.rb:10:in `sql_query'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_source.rb:120:in `prepare_for_render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/active_record/sql_source.rb:62:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration/index.rb:29:in `block in render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration/index.rb:29:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration/index.rb:29:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/core/index.rb:53:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration.rb:39:in `block in render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration.rb:39:in `collect'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.8/lib/riddle/configuration.rb:39:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/configuration.rb:78:in `render'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/configuration.rb:84:in `block in render_to_file'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/configuration.rb:84:in `render_to_file'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/rake_interface.rb:4:in `configure'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/rake_interface.rb:31:in `index'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bundler/gems/thinking-sphinx-ec658bff0479/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Kuba/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => ts:index
(See full trace by running task with --trace)
Try this,
ThinkingSphinx::Index.define :author, :with => :active_record do
indexes books.title, :as => :title
end
Edit:
Please check this issue in the git. https://github.com/pat/thinking-sphinx/issues/517

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.

Build associations for User favorites' other users

I want to build a simple favorite system. I've found tutorial for polymorphic associations on the web but i'm sure there is a lot more simple !!
So I have a User model and I want to let users have has many other users as favorites.
In the other way, a user may know who favored him.
I tried something like this:
class User < ActiveRecord::Base
has_many :favorites, :class_name => 'User', :foreign_key => 'user_id'
belongs_to :favorited_by, :class_name => 'User'
end
But I have an SQL error while doing
some_user.favorites << another_user
or
some_user.favorites.build(another_user)
some_user.save
some_user.favorites
Any help doing this simple thing... :)
Here is the trace:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.user_id: SELECT "users".* FROM "users" WHERE ("users".user_id = 1)
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:207:in `rescue in log'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/sqlite_adapter.rb:284:in `select'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/base.rb:473:in `find_by_sql'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/relation.rb:64:in `to_a'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:143:in `all'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:70:in `block in find'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/base.rb:1127:in `with_scope'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:63:in `find'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:468:in `find_target'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_collection.rb:398:in `load_target'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:145:in `inspect'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.9/lib/rails/commands/console.rb:44:in `start'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.9/lib/rails/commands/console.rb:8:in `start'
from /Users/pierrelouisgottfrois/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.9/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
Answering my own thx to #Dogbert
Your model associations are wrong. Have a look at my answer here and adapt it for your use case.

Resources