我已经堆积了。
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
@event.repeat_id = @event.id
@event.save
end
这是我的创建方法,我需要设置创建事件的repeat_id =创建事件的通常id。但是这段代码不起作用。有什么想法吗?
答案 0 :(得分:4)
试试这个
Utilities.formatDate(values[i][11], "GMT+1", "EEE dd.MM.yyyy")
在def create
@event = Event.new(event_params)
@event.user_id = current_user.id
@event.save
end
模型中添加event.rb
回调
after_create
希望这有帮助!
答案 1 :(得分:1)
你可以试试这个
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
if @event.save
@event.update_attributes(:repeat_id => @event.id)
@event.save
end
end
答案 2 :(得分:0)
您可以将其重组为:
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
@event.update_attributes(:repeat_id => @event.id) if @event.save
end
这里的关键点是,你不能拥有@event.id
,除非它存在于数据库中,即保存它。
答案 3 :(得分:0)
你必须做这样的事情:
def create
@event = Event.create(event_params)
@event.update_attributes(user_id: current_user.id, repeat_id: @event.id)
@event.save
end
您的事件在数据库中存在之前没有要复制的ID。
答案 4 :(得分:0)
使用此代码:
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
if @event.save
@event.update_column(:repeat_id,@event.id)
end
end