我想构建一个简单的收藏系统。我在网上找到了多态关联的教程,但我确信有更简单的方法!!
所以我有一个用户模型,我想让用户有很多其他用户作为收藏夹。 另一方面,用户可能知道谁更喜欢他。
我试过这样的事情:
class User < ActiveRecord::Base
has_many :favorites, :class_name => 'User', :foreign_key => 'user_id'
belongs_to :favorited_by, :class_name => 'User'
end
但是我在执行
时遇到SQL错误some_user.favorites << another_user
或
some_user.favorites.build(another_user)
some_user.save
some_user.favorites
任何帮助做这件事......:)
这是跟踪:
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'