0

我的控制器

new_task = {:user_id => params[:user_id],:task => params[:task] }
work = Work.new new_task
if work.valid?
    work.save!
    if !params[:sub].nil?
        new_sub = {:activity => self.activity.id,:sub_task => "something etc" }
        sub_task = SubTask.new new_sub
        sub_task.save!
    end

end

我的模特

attr_accessible :user_id, :task, :created_at, :updated_at
after_create :add_activity

def add_activity
    new_activity = {:user_id => self.user_id,:text => "new task" }
    user_activity = Activity.new new_activity
    user_activity.save!
end

我如何获得活动表单 after_create 回调的最后插入 ID。self.activity.id 不适合我

4

1 回答 1

1
def last_activity
  @last_activity || (Activity.where(:user_id => self.user_id).order("id").last if user_id)
end

def add_activity
  new_activity = {:user_id => self.user_id,:text => "new task" }
  user_activity = Activity.new new_activity
  user_activity.save!
  @last_activity = user_activity
end

self.last_activity.id
于 2012-12-29T18:52:37.547 回答