рельсы, postgres, devise. Существует база для боковых столов с корпорациями, к ней я успешно подключился. Цель: создать модель User, которая будет привязана к корпорации, чтобы корпорация могла войти в систему как пользователь. Я решил сделать соотношение через has_one.рельсы has_one через отношение не существует
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'
Что я делаю неправильно?
Показать, что содержит @corpor. –
@AlbertPaul, 'irb (main): 001: 0> @corpor = User.first Пользовательская загрузка (3,6 мс) SELECT« пользователи ». * FROM« пользователи »ORDER BY« пользователи ».« Id »ASC LIMIT 1 => # <Идентификатор пользователя: 1, логин: «medis», encrypted_password: «$ 2a $ 10 $ RTuziePKTeQIsOTDIVFvuOyDzWQZ2AifBFw9ABycoxx ...», remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2016-02-12 19:57:14", updated_at: "2016-02-12 19:57:14"> ' –
У вас есть 2 базы данных? Когда вы проходите через корпорацию, вы идете по этому дБ, но миграция создала таблицу в текущем db. Бьюсь об заклад, если вы обратились к нему через пользователя, он, вероятно, сработает. Не знаете, как получить Eround это – Doon