Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 10/18/11: accepts_nested_attributes_for validation gotchas

Ask for Help

“accepts_nested_attributes_for doesn’t validate nested records? When a form contains two of the same nested attributes and the values of these should cause a validation error (e.g., both have the same email, which has a uniqueness constraint), the form saves properly, but the nested objects are invalid when accessed later.”

accepts_nested_attributes_for only validates against the database, but the records in this example would only be in memory. The best solution is to handroll a validator on the parent that iterates over the nested attributes and checks the constraints.

“A Pivot was getting some errors using minified jQuery in IE8, which went away when he used the un-minified version. Any other IE8/jQuery gotchas?”

A few have been mentioned at standup in the past few weeks, most notably some versions of jQuery crash IE8 entirely.

“Is there a way to generate a png from html server-side?”

You can use a command-line tool like webkit2png or there is some rack middleware that can generate pdfs and maybe pngs from html

“A pivot had a page that was requesting images after the page loaded, but couldn’t find where they were coming from…”

You can check the originator column of assets in firebug or the chrome developer panel. You can also try overriding jquery ajax to see each call (which you may not want to do if you have a lot of ajax calls).

Comments
  1. josh susser says:

    It’s known that `validates_uniqueness_of` is not transactional. The safest way to deal with that is to add a unique index to the field in the database. You’ll get an error raised if you ever try to create a record with a duplicate value. I usually prefer to keep the business logic in the ruby code, but in this case I can cope with the redundancy for the sake of simplicity and safety.

  2. `accepts_nested_attributes_for` just sets your associated records’ attributes and makes sure autosave is true, it doesn’t explicitly play with anything validation-wise. You can validate these associated objects as well by adding `:validate => true` to the association (it’s `false` by default).

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

    (Your captchas are very difficult. D:)

  3. … and I didn’t read the problem properly. -ve points for me.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *