私は2つのクラス:Restream
とChannel
を持っています。チャネルには多くの再ストリームがあり、再ストリームは多くのチャネルに属している可能性があります。しかし、Restream
にはactive_channel
- これを使用しているチャンネルのIDも格納する必要があります。HABTMとhas_oneモデルエラー
class Restream < ActiveRecord::Base
has_one :active_channel, class_name: 'Channel'
has_and_belongs_to_many :channels
...
end
class Channel < ActiveRecord::Base
has_and_belongs_to_many :restreams
...
end
私が追加active_channel
ために書いたの移行:
add_column :restreams, :active_channel, :integer, index: true
add_foreign_key :restreams, :channels, column: :active_channel
それでも私はこれを取得、私はrails c
を実行し、Restream.last
を呼び出すたび:
irb(main):002:0> Restream.last
Restream Load (1.2ms) SELECT "restreams".* FROM "restreams" ORDER BY "restreams"."id" DESC LIMIT 1
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column channels.restream_id does not exist
LINE 1: SELECT "channels".* FROM "channels" WHERE "channels"."restr...
^
: SELECT "channels".* FROM "channels" WHERE "channels"."restream_id" = $1 ORDER BY "channels"."name" ASC LIMIT 1
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:637:in `prepare'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:637:in `prepare_statement'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:596:in `exec_cache'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:355:in `select'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/querying.rb:39:in `find_by_sql'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:639:in `exec_queries'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/association_relation.rb:32:in `exec_queries'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:515:in `load'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/relation.rb:243:in `to_a'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/associations/singular_association.rb:42:in `get_records'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/associations/singular_association.rb:57:in `find_target'
from /bundler_cache/gems/activerecord-4.2.5.1/lib/active_record/associations/association.rb:138:in `load_target'
... 4 levels...
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/ext/active_record.rb:45:in `each'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/ext/active_record.rb:45:in `inject'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/ext/active_record.rb:45:in `awesome_active_record_instance'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/formatter.rb:26:in `format'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/inspector.rb:137:in `unnested'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/inspector.rb:104:in `awesome'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/core_ext/kernel.rb:10:in `ai'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/core_ext/kernel.rb:20:in `ap'
from /bundler_cache/gems/awesome_print-1.6.1/lib/awesome_print/inspector.rb:31:in `output_value'
from /bundler_cache/gems/railties-4.2.5.1/lib/rails/commands/console.rb:110:in `start'
from /bundler_cache/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:in `start'
from /bundler_cache/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /bundler_cache/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /bundler_cache/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
が、私はそれは声明で失敗していること、が分かりましたSELECT "channels".* FROM "channels" WHERE "channels"."restream_id" = $1 ORDER BY "channels"."name" ASC LIMIT 1
私は何ですか間違っている? Thanx
はい、「has_one」を「belongs_to」に変更すると解決しました。 'belongs_to' =)でもうまくいくと思う。また、レールガイドに戻って問題があったのだが、今は' has_one'がなぜこのような 'id'を作るのか理解しようとする。私は実際には 'belongs_to'のようなidを作成すると期待しています。 – Ngoral
@Ngoral、 'belongs_to'、' has_one'、 'has_many'および' has_and_belongs_to_many'は、レコードを取得するために使用されるクエリのみを制御します。しかし、データベース内の実際の列の作成は、実行された移行までです。 'has_one'は特別です。コードを変更するだけで' has_many'にすることができます。なぜなら、本当に重要で外国関係を保持する 'belongs_to'だからです。'has_many'と' has_one'の唯一の違いは、 'has_one'が単一のレコードに限定された' SELECT ... LIMIT 1'クエリを実行することです。しかし、両方ともクエリのために他のモデルの 'belongs_to'を使います。 – fbelanger