how to check if parent exists in has many relation
I have 3 models:
User: has_many :comments
Video: has_many :comments
Comment: belongs_to :video, :user
videos/show.html.erb
<%= form_for(@comment, :method => :post ) do |f| %>
<%= f.text_area :body %>
<%= f.hidden_field :video_id, :value =>"4" %>
<%= f.submit "submit" %>
<% end %>
comments_controller
def create
@comment = Comment.new(params[:comment].merge(:user_id => current_user.id))
@comment.save
end
开发者_运维知识库
How can i check existence of Video before i create a comment?
This should be present as a validation in your Comment model.
class Comment < ActiveRecord::Base
validates_presence_of :video
精彩评论