1

I have two models user_item and user_item_images.

user_item.rb

has_many :user_item_images, dependent: :destroy
validates :user_item_images, presence: { message: "You must include a picture" }

user_item_images.rb

belongs_to :user_item

I have a nested form with only one user_item_image field which is :picture. When I submit an empty form I get this message

User item images You must include a picture

How do I make it so that the message instead says

You must include a picture

I don't know how to edit the en.yml file because the error is on the presence of another model and not an attribute of a model.

I looked here but the answer is too broad and I think I need a custom validation.

Community
  • 1
  • 1
user4584963
  • 2,403
  • 7
  • 30
  • 62
  • Possible duplicate of [Fully custom validation error message with Rails](http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails) – jeffdill2 Feb 27 '16 at 23:31
  • I don't think so. That answer is vague and I believe I needed a custom validation to solve my issue. – user4584963 Feb 28 '16 at 00:56
  • Yeah, you need custom validation. Check out these 2 answers on that question - http://stackoverflow.com/a/10635913/2266827, http://stackoverflow.com/a/33292528/2266827. They'll give you what you need. – jeffdill2 Feb 28 '16 at 03:16

3 Answers3

3

Create a custom validation instead:

has_many :user_item_images, dependent: :destroy
validate :has_a_picture

private

def has_a_picture
  errors.add(:base, 'You must include a picture') if user_item_images.none?
end 
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • Why should I still include the `presence: ...`? – user4584963 Feb 27 '16 at 21:53
  • I actually tried using your [monkey patch](http://stackoverflow.com/a/34347478/4584963). It worked great for the picture attribute on `user_item_images` doesn't work for presence of the association. – user4584963 Feb 27 '16 at 22:02
  • I was getting this error message `"undefined method 'none?' for :user_item_images:Symbol"` so I changed the method to `... if self.user_item_images.none?`. My only question is that I need to be certain that a `user_item` can never be created unless there is an associated `user_item_image`. Will this validation accomplish this? – user4584963 Feb 27 '16 at 22:16
  • I noticed your edit. Would the `self` in `self.user_item_images.none?` be redundant? – user4584963 Feb 27 '16 at 22:22
  • @user4584963 - it should prevent that on the application level, however there are other situation you need to be prepared for like user creating item with image and then deleting the image. It would be best to have some sort of db constraint to be 100% sure. – BroiSatse Feb 27 '16 at 22:22
  • @user4584963 - `self` is a default reciever and is only required to make a difference between local variable and method. Since there is no local variable with that name, `self` is not required. – BroiSatse Feb 27 '16 at 22:23
  • Some sort of db constraint as in another validation? Something like a `before_destroy` on `user_item_image` that checks to be sure there is at least one `user_item_image` associated? – user4584963 Feb 27 '16 at 22:26
-1

For that, in your user_item_images model you need: validates :picture, presence: true You may also want to look into whether you have data modeling issues to resolve. Regardless, your current validation only validates that the association exists. So, when you submit an user_item_images form with an empty picture field the validation that fails is the one in your user_item model. Rather than attempt to change the content of association presence validation error message, add a validation for the picture column in the user_item_images. That way if picture is empty, it will raise 'You must include a picture'.

I'll also comment that having the presence validation :user_item_images on the user_item model shouldn't be necessary and likely indicates a larger data modelling issue that you need to resolve.

alex1sz
  • 340
  • 4
  • 10
  • I think you might be confused as to how my models are set up. A `user_item` should never exist unless there is an associated `user_item_image`. The reason I have the message `"You must include a picture"` is because that is the only attribute a user is able to add to a `user_item_image`. – user4584963 Feb 27 '16 at 21:56
-3

There's no need to write extra code. You should use Rails' built in I18n to customise ActiveRecord error messages.

Inside config/locales/en.yml

en:
  activerecord:
    errors:
      models:
        user_item:
          attributes:
            user_item_images:
              presence: "You must include a picture"
Mohamad
  • 34,731
  • 32
  • 140
  • 219