みんなおはよう、
作業中の関連付けがありますが、何らかの理由で、IRB でデバッグしているときに問題のオブジェクトへの変更を保存できません。誰かが私の問題を指摘できるかどうか疑問に思っていました。
ここに協会があります:
class User < ActiveRecord::Base
has_and_belongs_to_many :affiliates
has_one :managed_affiliate, :class_name => "Affiliate", :foreign_key => "manager_id"
end
class Affiliate < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :manager, :class_name => "User"
#The affiliates table has a foreign key field of manager_id
end
IRB を起動すると、User.first と Affiliate.first を取得できます。ユーザーの managed_affiliate を問題なく設定できます。ただし、保存すると、それがアフィリエイトにまったく反映されません。マネージャーがいません。同様に、アフィリエイトのマネージャーも問題なく設定できます (Affiliate.first.manager = User.first)。すべて問題ないように返されますが、保存しようとすると単に「false」が返されます。IRB ログ機能を有効にすると、次のような出力が得られます。
SQL (0.1ms) BEGIN
SQL (0.2ms) ROLLBACK
この関連付けが適切に保存されない明らかな理由はありますか?
また、アフィリエイト テーブルのスキーマは次のとおりです。
create_table "affiliates", :force => true do |t|
t.string "name"
t.string "website"
t.integer "market_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "logo_file_name"
t.string "logo_content_type"
t.integer "logo_file_size"
t.boolean "track_subs"
t.integer "manager_id"
end
助けてくれてありがとう。