.new_record? and :_destroy

Rails version 2.3.8

I have a ticket model that has many comments

the comments have an attribute called “admin” – is this a comment
added by an administrator

the ticket has an attribute called “admin_email_address”

I want to validate when saving the ticket and say “if I have a new
admin comment, make sure I have an admin_email_address”

In my ticket model, the method to do the validate looks like the
following and works as expected except for one thing. If the :_destroy
flag on the comment was set in my comment view partial (I want to
delete a comment), it seems like this iterating over the comments with
new_record? somehow resets that :_destroy flag.

Am I doing something wrong here? Or, could someone give me a hint on a
more simple way to do this validation.?

Thanks !!

new_admin_comments = false
self.comments.each do |comment|
new_admin_comments = true if comment.new_record? &&
comment.admin_comment == true
end

if admin_email.blank? && new_admin_comments == true
errors.add_to_base(“Admin email address required.”)
end

jim <[email protected]…> writes:

Rails version 2.3.8

I have a ticket model that has many comments

the comments have an attribute called “admin” – is this a comment
added by an administrator

This seems weird. Why not simply have a comment owned by a user, which
may
have an admin flag?

the ticket has an attribute called “admin_email_address”

Again weird. Comments don’t have email addresses - administrators do.

I want to validate when saving the ticket and say “if I have a new
admin comment, make sure I have an admin_email_address”

I suggest you place conditional validations on the user model to ensure
administrators have an email address specified.

Thanks !!

new_admin_comments = false
self.comments.each do |comment|
new_admin_comments = true if comment.new_record? &&
comment.admin_comment == true
end

I am not sure how selecting ticket.comments will ever return
comment.new_record? since they are already in the database.

Anyway, why not handle this in the view? If the comment is owned by an
admin,
then alter the display.

if admin_email.blank? && new_admin_comments == true
errors.add_to_base(“Admin email address required.”)
end

This is not where it belongs. Try:

class User < ActiveRecord::Base
validates_presence_of :email, :if => :admin
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs