Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created April 13, 2012 20:13
Show Gist options
  • Select an option

  • Save steveklabnik/2379785 to your computer and use it in GitHub Desktop.

Select an option

Save steveklabnik/2379785 to your computer and use it in GitHub Desktop.
Did rails used to do this?

Using rails 3.2.2, btw.

1.9.3-p125 :005 > a = Article.first
 => #<Article id: 1, title: "Sample Article Title", body: "This is the text for my article, woo hoo!", created_at: "2012-04-13 00:16:04", updated_at: "2012-04-13 00:16:04"> 
1.9.3-p125 :006 > a.comments
 => [four comments] 
1.9.3-p125 :007 > c = a.comments.new
 => #<Comment id: nil, article_id: 1, author_name: nil, body: nil, created_at: nil, updated_at: nil> 
1.9.3-p125 :008 > a.comments
 => [four comments, #<Comment id: nil, article_id: 1, author_name: nil, body: nil, created_at: nil, updated_at: nil>] 

This becomes a problem when you want to do something like this:

  def show
    @article = Article.find(params[:id])
    @comment = @article.comments.new
  end

and in the view

<%= render :partial => 'comment', :collection => @article.comments %>
<h3>Post a Comment</h3>

<%= form_for @comment do |f| %>
...

Because you have to make the comment for the form, but then it tries to render it in the collection.

Is this new? Have I just never noticed it?

@oscardelben
Copy link
Copy Markdown

@claco I'm not sure I follow that (late here :)). In any case, you can always do this:

Comment.new({:article => @article)}, :without_protection => true)

@ezkl
Copy link
Copy Markdown

ezkl commented Apr 13, 2012

@technoweenie wrote a great blog post recently that explains how Github approached the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment