user_corporation.rb:
class UserCorporation < ActiveRecord::Base
belongs_to :user
belongs_to :corporation
end
user.rb:
class User < ActiveRecord::Base
devise :database_authenticatable,
:rememberable,
:trackable,
:timeoutable,
:authentication_keys => [:login]
has_one :user_corporation
has_one :corporation, :through => :user_corporation
end
corporation.rb:
class Corporation < OtherBaseconfiguration
self.table_name = 'Corporations'
has_one :user_corporation
has_one :user, :through => :user_corporation
end
20160212185152_create_user_corporations.rb:
class CreateUserCorporations < ActiveRecord::Migration
def change
create_table :user_corporations do |t|
t.belongs_to :user, index: true
t.belongs_to :corporation, index: true
t.timestamps null: false
end
end
end
rake db: migrate
已执行,表已创建。在控制台中,尝试获取公司用户,我收到错误:
irb(main):006:0> @corpor.corporation
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "user_corporations" does not exist
LINE 5: WHERE a.attrelid = '"user_corporations"'::reg...
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"user_corporations"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:733:in `column_definitions'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/schema_statements.rb:186:in `columns'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/schema_cache.rb:49:in `columns_hash'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/associations/association_scope.rb:85:in `column_for'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/associations/association_scope.rb:94:in `bind'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/associations/association_scope.rb:103:in `last_chain_scope'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.5/lib/active_record/associations/association_scope.rb:139:in `add_constraints'
... 13 levels...
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /home/marson/rails_project/infinity/bin/rails:9:in `<top (required)>'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/marson/.rvm/rubies/ruby-2.2.3/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
我做错了什么?
答案 0 :(得分:1)
也许我错过了它,但如果你只需要在公司和用户之间使用has_one到has_one,为什么要使用UserCorporations查找表呢?
答案 1 :(得分:1)
您的设计存在缺陷,您尝试以错误的方式使用has_one并且失败了。您需要以下设计之一: 的 A 强>
公司有很多用户 用户属于公司
(一个用户只属于一个公司,一个公司可以有很多用户)。
<强>乙强>
公司有很多UserCorporations 用户有很多UserCorporations UsedCorporation属于User UserCorporation属于公司
(这是多对多的,因此用户可以属于多个团队,而公司可能有多个用户)
<强> C 强>
公司所属的用户 用户有很多公司
(用户连接到多个团队,而Corp只能属于单个用户)
答案 2 :(得分:1)
使用has_many :through
执行此操作
如
(公司拥有许多用户belongs_to
公司)
公司belongs_to
用户用户has_many
公司
(用户连接到多个公司,公司只能belong_to
单个用户)