Having an issue with a foregin key not getting updated in rails.
I have an application with 3 models.
User, Reviews and Comments.
Users have many reviews and reviews have many comments. When I add a new comment I want to add the User_id and the Review_id to the comments table.
The comments are passed from a form. I update the user_id in the following statement
@comment = current_user.comments.build(params[:comment]) in the comment controller
Everything gets instered into the Comments table apart from the reviews_id which is the foregin key. It was my understanding that this would be done automatically through the associations in the models. Is this not the case or am I missing something in my models.
Users Model
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :reviews, dependent: :destroy
has_many :comments, dependent: :destroy
end
Reviews Model
class Review < ActiveRecord::Base
attr_accessible :review_text, :tumb_up_down, :rating, :name
belongs_to :user
has_many :comments, dependent: :destroy
Comments Model
class Comment < ActiveRecord::Base
attr_accessible :comments, :review_id, :user_id
belongs_to :reviews
The comments controller code
def create
@comment = current_user.comments.build(params[:comment])
if @comment.save
redirect_to 'static_pages/home' #{:action => 'show', :id }
else
redirect_to 'static_pages/home'
end
end
And finally the form that passes the values. Should I maybe be including the review_id in this.
<div class="field">
<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :comments %>
<%= f.text_area :comments, placeholder: "Add your words of wisdom" %>
</div>
<%= f.submit "Add", class: "btn btn-large btn-primary" %>
<% end %>