I'm still familiarizing myself with Rails and have a question in regards to a problem I'm trying to solve utilizing Rails 3.2. Here is a link to the few files in question where I assume my problem lies:
https://gist.github.com/0fe5cf9093b21f4f632f
I apologize if the code is really bad/adds a few things that don't need to exist as I'm still trying to get used to how everything works as I go along.
I want to have comments for each post that is made on my website - and the logic I have is that users have many comments, and I suppose that the posts have many comments through the users. I do have that relationship added (though it doesn't seem to be doing anything in particular), and I feel as if all of my table references are correct (though I haven't added anything to users as I'm using devise and didn't feel like I needed to add anything as the post is a separate object itself).
As of right now, I can show all of the comments in general, as in I am able to create and save them. I was also saving, at the very least, the user_id
variable, so it can understand which account is making the comment. I'm not really sure where/when I should be passing in the post_id
, so that's the biggest issue. Any way I've tried to do it in the posts_controller
(since in theory I should be able to edit that attribute when the post page is loaded), I get an
undefined method 'post_id'
or
no post_id column
error, indicating that it doesn't exist, even though as far as I understand, I've defined it. The fact that the user_id
is able to be saved, as mentioned before, further confuses me.
If I show the attributes on a comment created in my show view, I get the following:
{
"id"=>1,
"content"=>"text here",
"posted"=>Sat, 03 Nov 2012 20:36:53 UTC +00:00,
"user_id"=>1,
"created_at"=>Sat, 03 Nov 2012 20:36:53 UTC +00:00,
"updated_at"=>Sat, 03 Nov >2012 20:36:53 UTC +00:00
}
There is no post_id
, although I did add it into the migration table for comments. Trying to pass the @post.id
in the posts controller (to a show method's instance of a comment, as it would be available beneath a post) gives a mass assignment error. Messing around with the :class
and :foreign_key
options hasn't done much, either. Based on the research I have done, which includes some of Rails' own documentation, I don't believe I need a has_many
through relationship, but I don't know where I'm missing what I need to have the information save accordingly.
Perhaps this is all due in part to my lacking understanding of database/model relationship design, although any help is appreciated. I have seen a couple of similar questions but I still find it difficult to apply their solutions to the context of my own particular issue.
Based on your post, it sounds like you may have added post_id to the migration after you created the table. If this is true, please understand that it is bad practice to modify a migration file. Instead, you should create a new migration that will "fix" the status of your database.
It looks like your relationships are setup correctly for posts and comments. The model with "belongs_to" will have the foreign key; as such, I would expect comments to have "post_id."
Please see if the following works for you:
rails g migration addPostIdToComments
add_column :comments, :post_id, :integer
This should add the post_id column to your comments table.
If you get in a bind and want to completely destroy your database and recreate it from scratch (it looks like a sample application), (please note this will completely drop the database) you may run: rake db:drop db:create db:migrate
The mass assignment issue may be related to a new security option in rails which requires whitelisting all model attributes (columns) that can be updated in the database. Please see the attr_accessible documentation for whitelisting attributes: http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html
Welcome to rails! Hope this helps!