我有一个名为的操作update_mobile
,其中我正在准备在 RABL JSON 请求中使用的大量实例变量。可以看到,如果请求中包含params[:last_updated]
参数,那么我只获取到了此后更新的模型。这似乎违反了“胖模型,瘦控制器”的方法。我怎样才能重构这个方法?
def update_mobile
@last_updated = params[:last_updated]
if @last_updated.nil?
@buddies = @user.friends
@courses = @user.courses
@friendly_schools = @user.friendly_schools
@documents = @user.all_notes
@instructors = @user.current_instructors
@friendships = @user.friendships
@questions = @user.current_questions
@answers = @user.current_answers
@comments = @user.current_comments
else
@buddies = @user.friends.select{ |user| user.updated_at > @last_updated }
@courses = @user.courses.select{ |course| course.updated_at > @last_updated }
@friendly_schools = @user.friendly_schools.select{ |school| school.updated_at > @last_updated }
@documents = @user.all_notes.select{ |note| note.updated_at > @last_updated }
@instructors = @user.current_instructors.select{ |instructor| instructor.updated_at > @last_updated }
@friendships = @user.friendships.select{ |friendship| friendship.updated_at > @last_updated }
@questions = @user.current_questions.select{ |question| question.updated_at > @last_updated }
@answers = @user.current_answers.select{ |answer| answer.updated_at > @last_updated }
@comments = @user.current_comments.select{ |comment| comment.updated_at > @last_updated }
end
end