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.
Related
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).
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
User has_many comments. Comment are in the comments table (polymorphic).
User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).uniq
I get:
Post Load (0.4ms) SELECT `posts`.* FROM `posts` LIMIT 1
TypeError: Cannot visit Arel::Nodes::Distinct
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:25:in `rescue in visit'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:133:in `visit_Arel_Nodes_SelectCore'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:41:in `visit_Arel_Nodes_SelectCore'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `block in visit_Arel_Nodes_SelectStatement'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `map'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:121:in `visit_Arel_Nodes_SelectStatement'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/mysql.rb:36:in `visit_Arel_Nodes_SelectStatement'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:19:in `visit'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/visitor.rb:5:in `accept'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/to_sql.rb:19:in `accept'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/arel-3.0.2/lib/arel/visitors/bind_visitor.rb:11:in `accept'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/explain.rb:40:in `logging_query_plan'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/querying.rb:37:in `find_by_sql'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:171:in `exec_queries'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:160:in `block in to_a'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/explain.rb:33:in `logging_query_plan'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:159:in `to_a'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/activerecord-3.2.3/lib/active_record/relation.rb:496:in `inspect'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/.rvm/gems/ruby-1.9.3-p125#myapp/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
Without .uniq, it works, except I get duplicates, therefore, I can try:
User.where("comments.commentable_type = ? AND comments.commentable_id = ?", "Post", post.id).to_a.uniq
And it will work...but I don't like that method! Is there any other way?
Restart the rails server session.
This is a link to the github issue related to the same problem that you face.
If you are using rails 4 try using .distinct instead of .uniq .
I remember having issues similar, couple things you can do. First use the SQL distinct (I know it is suppose to be similar to the uniq). Second (I know this is not the best way), you can just select it all then just use regular array uniq functions to filter it just like how you would filter a normal array (this is to just get things going).
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.
Is there anyway of defining facet on an associated model's column which is of type int?Does sphinx convert all fields defined as facets to strings if they are of an associated models columns?
I am trying to define a column of my child table as an facet.After
rebuilding when I do Model1.facets i get this error.
class Model1 < ActiveRecord::Base
....
has_many :communities, :through => :model2
...
define indexes
...
indexes model2.community_id, :as => :community_id, :facet => :true
...
end
end
model 1 - Current model
model 2 - join model using :through
Model1.facets
NoMethodError: undefined method `to_crc32' for 9:Fixnum
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:99:in `translate'
from (irb):3:in `detect'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:98:in `each'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:98:in `detect'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:98:in `translate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet.rb:75:in `value'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:114:in `add_from_results'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:219:in `each_with_groupby_and_count'
from (irb):3:in `each_with_index'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:218:in `each'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:218:in `each_with_index'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search.rb:218:in `each_with_groupby_and_count'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:113:in `add_from_results'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:49:in `populate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:47:in `each'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:47:in `populate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/facet_search.rb:13:in `initialize'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search_methods.rb:422:in `new'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/
lib/thinking_sphinx/search_methods.rb:422:in `facets'
from (irb):3>>
If you're using integers for facets, then they should be defined as attributes:
has model2.community_id, :as => :community_id, :facet => :true
The indexes method is for fields - which are the textual data you want people to be able to search for. I'm guessing the people who use your site won't be entering in the community id into the search field. Leave a comment if that's not the case, and I'll expand on how you can have the community_id as both a field and an attribute.