# Before\_create question

**URL:** <https://www.ruby-forum.com/t/before-create-question/101452>\
**Category:** Rails\
**Created:** [December 30, 2005, 7:39am UTC](https://www.ruby-forum.com/t/before-create-question/101452 "2005-12-30T07:39:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 30, 2005, 7:39am UTC](https://www.ruby-forum.com/t/before-create-question/101452/1 "2005-12-30T07:39:52Z")

</div>

I have the following models: User, Registration, Event.

User has\_many Registration, Event has\_many Registration, and  
Registration belongs\_to User and Registration. So far, so good.

There’s a registration deadline on a Event though, a few days before  
the Event occurs.

So, I have this:

class Registration \< AR  
def before\_create  
errors.add\_to\_base(“Registration deadline has passed”) unless  
event.can\_still\_register?  
end  
end

The weird thing is, if the registration deadline has passed, the error  
gets added to the registration object, and calling save! throws no  
exception, but the registration object doesn’t get saved.

So, this test code throws no exception:

```
event = TrainingEvent.new :start_date => 19.days.from_now,
                          :end_date => 22.days.from_now,
                          :capacity => 10

reg = Registration.create :training_event => event, :user => @bob,

```

:role =\> @community\_director, :program\_number =\> ‘asdf’  
assert\_equal reg.errors.on(:base), ‘Registration deadline has  
passed’  
reg.save!

I’m asserting that errors exist, but save! doesn’t throw an exception.  
But the registration doesn’t seem to be saved.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 30, 2005, 8:25am UTC](https://www.ruby-forum.com/t/before-create-question/101452/2 "2005-12-30T08:25:02Z")

</div>

Joe Van D. wrote:

> class Registration \< AR  
> def before\_create  
> errors.add\_to\_base(“Registration deadline has passed”) unless  
> event.can\_still\_register?  
> end  
> end
> 
> The weird thing is, if the registration deadline has passed, the error  
> gets added to the registration object, and calling save! throws no  
> exception, but the registration object doesn’t get saved.

before\_create is too late in the chain to add errors that affect the  
validation check. You’ll need to rename to validate\_on\_create.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [December 30, 2005, 10:48am UTC](https://www.ruby-forum.com/t/before-create-question/101452/3 "2005-12-30T10:48:23Z")

</div>

On 12/29/05, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > gets added to the registration object, and calling save! throws no  
> > exception, but the registration object doesn’t get saved.
> 
> before\_create is too late in the chain to add errors that affect the  
> validation check. You’ll need to rename to validate\_on\_create.

Ah, thanks. Now my tests pass!
