0

我正在尝试清理我的控制器,我认为最好将此逻辑保留在模型中:

@current_issue = Issue.order(:id).last
@doc = Doc.new do |n|
  n.user_id = current_user.ftp
  n.season = Date::MONTHNAMES[Time.now.month].to_s + " " + Time.now.year.to_s
  n.article1_body = @current_issue.articles[0].body
  n.article2_body = @current_issue.articles[1].body
  n.article3_body = @current_issue.articles[2].body
  n.cover = @current_issue.cover
  n.message = @current_issue.message
  n.issue_code = @current_issue.issue_code
end
@issues = Issue.get_em_boys

这样做的最佳方法是什么?

4

1 回答 1

3

作为一个起点...

控制器:

@doc = Doc.new
@doc.generate_from_issue(current_user, @current_issue)
...
@issues = Issue.get_em_boys

模型:

def generate_from_issue(user, issue)
  self.user_id = user.ftp
  self.season = Date::MONTHNAMES[Time.now.month].to_s + " " + Time.now.year.to_s
  self.article1_body = issue.articles[0].body
  self.article2_body = issue.articles[1].body
  self.article3_body = issue.articles[2].body
  self.cover = issue.cover
  self.message = issue.message
  self.issue_code = issue.issue_code 
end
于 2012-09-25T04:35:23.107 回答