1

I've been using accepts_nested_attributes_for for a few different models and I've got an odd situation.

I can skip creation blanks thru the top model, and I can validate_presence of individual records thru the bottom, but is it possible to do a most complex validation on a set of records?

I have the models Rooms and Rates. Rooms has_many Rates and accepts_nested_attributes_for Rates.

I can weed out blanks with

accepts_nested_attributes_for :room_rates, :reject_if => lambda { |a| a[:price].blank? }

but for each Room there are 7 different records (rates).. always 7 and submitted at once from the same form. Currently if they enter only 1 day it will allow it. However I would like to validate that all 7 must exist. Either they enter all 7 and it accepts it or it gives them an error for entering only 1 or 2. And does nothing at all if they leave all 7 completely blank.

I haven't seen anything like this yet for nested_attirbutes so i'm wondering if it's possible.

1 Answer 1

1

I'm not sure this would work, but can you not check the size of the rates in your Room class, something like:

class Room < ActiveRecord::Base
  validate :all_rates_present

...

private
def all_rates_present
  unless rates.size == 7
    errors.add(:rates, "must all be filled in.")
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.