# Problem with adding a new column to table

**URL:** <https://www.ruby-forum.com/t/problem-with-adding-a-new-column-to-table/172984>\
**Category:** Rails\
**Created:** [July 30, 2009, 8:36pm UTC](https://www.ruby-forum.com/t/problem-with-adding-a-new-column-to-table/172984 "2009-07-30T20:36:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jj46](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@jj46](https://www.ruby-forum.com/u/jj46)\
**Post date:** [July 30, 2009, 8:36pm UTC](https://www.ruby-forum.com/t/problem-with-adding-a-new-column-to-table/172984/1 "2009-07-30T20:36:32Z")

</div>

hi everyone.  
ok i have a little problem. I created a simple blog + comments system. i  
followd ryan bates tutorial on that one, maybe you’ve checked that  
screencast out once.  
anyway, i added a new colum to my comments table by entering “ruby  
script/generate migration add\_name\_to\_comment name:string”

and when i look in my migrations file, it seems to have worked:  
class AddNameToComment \< ActiveRecord::Migration  
def self.up  
add\_column :comments, :name, :string  
end

def self.down  
remove\_column :comments, :name  
end  
end

I added “name” to every views-comments page:  
-----------\> edit.html.erb:  
\<% form\_for(@comment) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :post\_id %\>  
 \<%= f.text\_field :post\_id %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

\<%= f.label :body %\>  
 \<%= f.text\_area :body %\>

\<%= f.submit 'Update' %\>

\<% end %\>

---------\>index.html.erb

 Post Name Body 

\<% @comments.each do |comment| %\>

 \<%=h comment.post\_id %\> \<%=h comment.name %\> \<%=h comment.body %\> \<%= link\_to 'Show', comment %\> \<%= link\_to 'Edit', edit\_comment\_path(comment) %\> \<%= link\_to 'Destroy', comment, :confirm =\> 'Are you sure?', :method =\> :delete %\> \<% end %\>

------------\>new.html.erb  
\<% form\_for(@comment) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :post\_id %\>  
 \<%= f.text\_field :post\_id %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

\<%= f.label :body %\>  
 \<%= f.text\_area :body %\>

\<%= f.submit 'Create' %\>

\<% end %\>

-------------\>show.html.erb

**Post:** \<%=h @comment.post\_id %\>

**Name:** \<%=h @comment.name %\>

**Body:** \<%=h @comment.body %\>

So far, so good.  
I have tagged comments on the post-pages by adding “has\_many” to the  
post model, and “belongs\_to” to the comment model.

and I use an AJAX remote\_form\_for for putting the comments-creator on  
the posts’ show page:  
\<%= render :partial =\> @post %\>

## Comments

 \<%= render :partial =\> @post.comments %\>

\<% remote\_form\_for [@post, Comment.new] do |f| %\>

\<%= f.label :name, "Name" %\>  
 \<%= f.text\_field :name %\>  
  
 \<%= f.label :body, "New comment" %\>  
 \<%= f.text\_area :body %\>

\<%= f.submit "Add comment" %\>

\<% end %\>

Alright. i hope yall can follow. Naturally, i also have a create.js.rjs  
file and put javascript on my layouts.rhtml-file.

And lastly, i have a \_comment.rhtml file for displaying the comments  
under the post on the posts-show page:  
\<% div\_for comment do %\>

**Posted \<%= time\_ago\_in\_words(comment.created\_at) %\> ago by \<%= h(comment.name)%\>**    
 \<%= h(comment.body)%\>

\<% end %\>

That’s that i want to be displayed on my show-post-page. When the  
comment was posted, the name of the creator, and the actual comment.

but for some reason it doesn’t display the newly added “name”, yet it  
displays EVERY other column in the comments-table. if put  
\<%= h(comment.body) instead of \<%= h(comment.name), it displays the  
comment again, or if i put \<%= h(comment.created\_at) instead of \<%=  
h(comment.name), it displays the time it was created. that all works,  
EXEPT the name. what’s up with that, i cannot figure out the problem…

I hope my problem isn’t too confusing to understand, and i hope i posted  
all relevant code associated to the problem. if i missed some code you  
need, just tell me i’ll post it.

JJ

---

<div class="post-metadata">

**Author:** ![jj46](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@jj46](https://www.ruby-forum.com/u/jj46)\
**Post date:** [July 30, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/problem-with-adding-a-new-column-to-table/172984/2 "2009-07-30T21:10:42Z")

</div>

J46 S46 wrote:  
[…]

> anyway, i added a new colum to my comments table by entering “ruby  
> script/generate migration add\_name\_to\_comment name:string”
> 
> and when i look in my migrations file, it seems to have worked:  
> class AddNameToComment \< ActiveRecord::Migration  
> def self.up  
> add\_column :comments, :name, :string  
> end
> 
> def self.down  
> remove\_column :comments, :name  
> end  
> end

Yes, you’ve got the correct code in your migration file. Now…did you  
remember to run the migration?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jj46](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@jj46](https://www.ruby-forum.com/u/jj46)\
**Post date:** [July 30, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/problem-with-adding-a-new-column-to-table/172984/3 "2009-07-30T21:15:39Z")

</div>

Marnen Laibow-Koser wrote:

> J46 S46 wrote:  
> […]
> 
> > anyway, i added a new colum to my comments table by entering “ruby  
> > script/generate migration add\_name\_to\_comment name:string”
> > 
> > and when i look in my migrations file, it seems to have worked:  
> > class AddNameToComment \< ActiveRecord::Migration  
> > def self.up  
> > add\_column :comments, :name, :string  
> > end
> > 
> > def self.down  
> > remove\_column :comments, :name  
> > end  
> > end
> 
> Yes, you’ve got the correct code in your migration file. Now…did you  
> remember to run the migration?
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Yeah, i did. sorry, forgot to mention that…

---

<div class="post-metadata">

**Author:** ![jj46](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@jj46](https://www.ruby-forum.com/u/jj46)\
**Post date:** [July 31, 2009, 1:49am UTC](https://www.ruby-forum.com/t/problem-with-adding-a-new-column-to-table/172984/4 "2009-07-31T01:49:09Z")

</div>

Sorry if this sounds primitive but if there was a problem with your  
migration your form\_for would throw an error about missing the name  
field, so I suspect something is cached or the server needs a restart

I would guess that you either need to restart your mongrel server, or  
whichever server you have running the application, and you ran rake  
db:migrate right? What server are your running under?

Otherwise I wouldtry  
script/console

and then just type

> Comment  
>   
> or  
> Comment.new  
>   
> see if the name attribute is there.

Also I would try adding some other text to your erb files to make sure  
they are updating…?
