SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Should I do this in the controller or model?

    I have an Article class that has a belongs_to relationship with an Author class.
    My article form has a list of existing authors to choose from which if one is selected will insert the author_id for the article. It also has the option to create a new author. Where should I put the code to create the author? I currently have it in the ArticleController, where I check for the existance of a variable and if it exists I create a new Author and push the author id to the params for creating the Article.
    It doesnt feel right, seems like the Article model should do the work in it's constuctor.
    Any ideas or suggestions would be great, thanks.

  2. #2
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To me from what I understand, it is something that should be put in the controller since it depends on input from a form.

    Something like (in the controller)

    Code:
    if params[:create_new_author]
      article.create_author(:name => 'John Doe')
    else
      article.author = Author.find(params[:author_id])
    end
    Ohai!

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the controller, using code along the lines of above.

  4. #4
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •