Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validates :presence vs validates_presence_of using rails 3

Tags:

I have a couple of simple models that are associated like so:

MODELS

class Task < ActiveRecord::Base
  belongs_to :user
  validates :name, :presence => true, :message => 'Name cannot be blank, Task not saved'
end

class User < ActiveRecord::Base
  has_many :tasks
end

VIEW has a call in it like so: user.tasks <-- then I loop through the tasks

The Issue:

In the task model --

when I use:

validates :name, :presence => true ,  :message => 'Name cannot be blank, Task not saved'

I get a 500 error:

ActionView::Template::Error (uninitialized constant User::Task):
NameError in View file

when I use:

validates_presence_of :name

Everything works.

I thought the both validates methods above where the same...is the issue have to do with associations and how validation tie into associated models. I have a hunch that something is going on with the way things are associated, but it is just a hunch.

Any help will be appreciated. Thank very much.

like image 458
RidingRails Avatar asked Feb 22 '11 13:02

RidingRails


People also ask

How do I validate in Ruby on Rails?

This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the :with option. Alternatively, you can require that the specified attribute does not match the regular expression by using the :without option. The default error message is "is invalid".

Can we save an object in DB if its validations do not pass?

If any validations fail, the object will be marked as invalid and Active Record will not perform the INSERT or UPDATE operation. This helps to avoid storing an invalid object in the database. You can choose to have specific validations run when an object is created, saved, or updated.

What is validate in rails?

In Rails, validations are used in order to ensure valid data is passed into the database. Validations can be used, for example, to make sure a user inputs their name into a name field or a username is unique.

What happens on Save rails?

So, save! won't just return true or false but only true on success and raise an excpetion if it fails. The purpose of this distinction is that with save! , you are able to catch errors in your controller using the standard ruby facilities for doing so, while save enables you to do the same using standard if-clauses.


1 Answers

When you use the newer validates :name format, you can put multiple validations in one line rather than having to have multiple lines for each type of validation. Because of this, when Rails hits your :message parameter, it thinks it's a validation method rather than a message associated with :presence. Try this instead:

validates :name, :presence => {:message => 'Name cannot be blank, Task not saved'}

Also, depending on how you display your errors, this error may actually show up as 'Name Name cannot be....'; if so, you'll want to set the message to just 'cannot be blank, Task not saved'.

like image 199
Dylan Markow Avatar answered Feb 23 '23 04:02

Dylan Markow