Rails:模型或控制器中的逻辑?

时间:2012-09-25 03:32:07

标签: ruby-on-rails-3 model controller

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

@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

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

1 个答案:

答案 0 :(得分: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