I have two problems. I have a comment that has_many uploads. Before
saving the comment, I want to be sure that the upload(s) has passed
validation, but I also need to validate in other ways. For example, I
do not want to save the comment if there is no comment or upload. Or, I
do not want to save the comment if the image has been uploaded
previously (comparing md5s with past upload md5s within the scope of an
IP).
Previously I had all of the upload attributes combined with my Comments
table, which made validation extremely easy. Then when I decided that I
would allow multiple uploads per comment, I had to separate the uploads
and comments into two different tables, which created the problems I am
describing.
Models
class Comment < ActiveRecord::Base
belongs_to :board
has_many :uploads, :dependent => :destroy
end
class Upload < ActiveRecord::Base
belongs_to :comment
end
Controller
def index
if request.post?
comment = @board.comments.build(params[:comment])
if comment.save
upload = comment.uploads.create(params[:upload])
redirect_to "/#{@board.path}"
end
end
end