私は 2 つのモデルを持っています。それらを更新しているときにエラーが発生します。ネストされた属性を使用しました。
class Channel < ActiveRecord::Base
self.primary_key = 'id'
has_many :channel_mappings , primary_key: 'channel_name', foreign_key: 'channel_name'
attr_accessible :channel_name, :channel_mappings_attributes
validates_presence_of :channel_name
accepts_nested_attributes_for :channel_mappings, :allow_destroy => true
end
2ndモデル
class ChannelMapping < ActiveRecord::Base
self.primary_key = 'src_channel'
belongs_to :channel, primary_key: 'channel_name', foreign_key: 'channel_name'
attr_accessible :src_channel, :channel_name , :src_file_type
end
Update メソッド
def update
@channel = Channel.find(params[:id])
if @channel.update_attributes(params[:channel])
redirect_to @channel, notice: 'Channel was successfully updated.'
else
render action: 'edit'
end
end
エラー
Type: ActiveRecord::RecordNotFound
Message: Couldn't find ChannelMapping with ID=ANY NAME for Channel with ID=2
主キーの上書きに関係していることはわかっています。どんな助けも役に立ちます
デシベル/schema.rb
create_table "channels", :force => true do |t|
t.text "channel_name", :null => false
t.string "internal_flag", :limit => nil
t.string "exception_flag", :limit => nil
end
create_table "channel_mappings", :id => false, :force => true do |t|
t.text "src_channel", :null => false
t.text "channel_name", :null => false
end