Forum: Ruby on Rails Rails 4 Postgres Array attribute: how can I ensure that the attribute is an array during validation?

04802f2354cf1b546e3aaf8060ba6ef9?d=identicon&s=25 Moncef Belyamani (Guest)
on 2014-04-11 17:19
(Received via mailing list)
Hi,

I just started experimenting with the native Postgress array type in
Rails
4, and I'm running into an issue I can't figure out. I want the app to
raise an error if the value for an array attribute is not an array.

Before, when I was using a regular text type for the field, and
serializing
it with Rails, I was able to make sure the attribute was an array with a
custom validation like this:

def format_of_admin_email regexp = /.+@.+\..+/i if admin_emails.present?
&&
(!admin_emails.is_a?(Array) || admin_emails.detect { |a|
a.match(regexp).nil? }) errors[:base] << "admin_emails must be an array
of
valid email addresses" end end

But now that the field is a Postgres array, it seems like Rails
automatically converts the string input into an empty array, so the
validation never fails. Here's my migration:

class AddAdminEmailsToLocations < ActiveRecord::Migration def change
add_column :locations, :admin_emails, :text, array: true end end

If I create a Location where admin_emails is a string:

Location.create!(admin_emails: "this should fail")

it doesn't raise a validation error, and it sets admin_emails to an
empty
array.

Am I doing something wrong or is this a bug in Rails?

I also tried checking if the input is an array in a before_validation
callback, and added a puts admin_emails.present?, but it returned false,
as
if it never saw the String input.
52f3528c40e9cf28ad0900886eecb128?d=identicon&s=25 Jordon Bedwell (Guest)
on 2014-04-11 22:30
(Received via mailing list)
Try adding `return false`

Thanks,

Jordon Bedwell
https://envygeeks.com
On 4/11/2014 10:18:09 AM, Moncef Belyamani
<moncef@codeforamerica.org>wrote:
Hi,
I just started experimenting with the native Postgress array type in
Rails 4, and I'm running into an issue I can't figure out. I want the
app to raise an error if the value for an array attribute is not an
array.

Before, when I was using a regular text type for the field, and
serializing it with Rails, I was able to make sure the attribute was an
array with a custom validation like this:
def format_of_admin_email regexp = /.+@.+\..+/i if admin_emails.present?
&& (!admin_emails.is_a?(Array) || admin_emails.detect { |a|
a.match(regexp).nil? }) errors[:base] << "admin_emails must be an array
of valid email addresses" end end
But now that the field is a Postgres array, it seems like Rails
automatically converts the string input into an empty array, so the
validation never fails. Here's my migration:
class AddAdminEmailsToLocations < ActiveRecord::Migration def change
add_column :locations, :admin_emails, :text, array: true end end
If I create a Location where admin_emails is a string:
Location.create!(admin_emails: "this should fail")
it doesn't raise a validation error, and it sets admin_emails to an
empty array.
Am I doing something wrong or is this a bug in Rails?
I also tried checking if the input is an array in a before_validation
callback, and added a puts admin_emails.present?, but it returned false,
as if it never saw the String input.
--
You received this message because you are subscribed to the Google
Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to rubyonrails-talk+unsubscribe@googlegroups.com
[mailto:rubyonrails-talk+unsubscribe@googlegroups.com].
To post to this group, send email to rubyonrails-talk@googlegroups.com
[mailto:rubyonrails-talk@googlegroups.com].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk...
[https://groups.google.com/d/msgid/rubyonrails-talk...].
For more options, visit https://groups.google.com/d/optout
[https://groups.google.com/d/optout].
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.