0

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 %>
Andrew
  • 1
  • 3
  • Your review model is missing an attr_accessible parameter for :user_id and secondly, if Comments belong to Reviews, why are you creating comments via your User model? – regulatethis Jan 06 '13 at 23:48
  • I am not sure what you mean by creating comments through the User Model. I have the association there as Comments are associated to Users. Can this be down through the reviews model instead? Sorry if this is a basic question but I am very new to rails. – Andrew Jan 07 '13 at 00:43
  • Taking out the has_many :comments, dependent: :destroy from the user model breaks the command @comment = current_user.comments.build(params[:comment]) in the comment controller. I use this as I also want to place the user_id into the comments table. – Andrew Jan 07 '13 at 00:53
  • Can I use something like @comment = @review.comments.build(params[:comment]) in my comments create controller to create the new comment through the reviews model. Would this update the foreign key. – Andrew Jan 07 '13 at 02:23
  • This post http://stackoverflow.com/questions/5268254/rails-pass-id-parameter-on-a-link-to gave me the solution to the problem. I passed the review object to the comments controller and was able to include the review id on the new comments form. – Andrew Jan 07 '13 at 05:55

0 Answers0