问题描述:

I'm trying to clean out my controller, and I figured it'd be best to keep this logic in the model:

@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

What's the best way to go about doing that?

网友答案:

As a starting point...

Controller:

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

Model:

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
相关阅读:
Top