i have 2 models, face error when updating them. have used nested attributes.
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 model
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 method
def update @channel = channel.find(params[:id]) if @channel.update_attributes(params[:channel]) redirect_to @channel, notice: 'channel updated.' else render action: 'edit' end end
error
type: activerecord::recordnotfound message: couldn't find channelmapping id=any name channel id=2
i know it' primary key overwritten. useful
db/schema.rb
create_table "channels", :force => true |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 |t| t.text "src_channel", :null => false t.text "channel_name", :null => false end
you try - @channel.attributes = params[:channel]
instead of @channel.update_attributes(params[:channel])
this set attributes without save.
then can call -
@channel.save
this save attributes.
the error seems record not found rather update revert like.
check error log first , if needed post here if nothing works.
it better use if else conditions as:
if @channel.save #record saved else #error in save end
then can know it's going.
Comments
Post a Comment