0

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?

t56k
  • 6,769
  • 9
  • 52
  • 115
  • Can you please explain what the code is doing ? – Abhay Kumar Sep 25 '12 at 03:58
  • It's filling out values of a form from a different model. – t56k Sep 25 '12 at 04:06
  • If there is some code that has nothing to do with the view directly, then u can put that code in the model. It is basically used by the controller to so some task related to that model. – Abhay Kumar Sep 25 '12 at 04:16
  • Yeah, it has something to do with the view directly: it populates the form. Should it stay in the controller then? – t56k Sep 25 '12 at 04:19
  • 1
    It must at least to my knowledge. or you can define a method inside a model that does the above work and returns the result to the controller which in turn call the view – Abhay Kumar Sep 25 '12 at 04:24
  • 1
    [this](http://stackoverflow.com/questions/60658/rails-model-view-controller-and-helper-what-goes-where) link could help you. – Abhay Kumar Sep 25 '12 at 04:28

1 Answers1

3

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
John
  • 4,362
  • 5
  • 32
  • 50