# Fat models and validation

**URL:** <https://www.ruby-forum.com/t/fat-models-and-validation/122371>\
**Category:** Rails\
**Created:** [December 7, 2007, 12:20am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371 "2007-12-07T00:20:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 12:20am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/1 "2007-12-07T00:20:45Z")

</div>

Hi all,

I’m trying my best to adhere to the thin controller/fat model  
philosophy - in particular, I’m trying to make sure my validation  
stays inside my models, so it can be reused in multiple places.

My current problem is that I have an action that creates several  
objects all at once. In my app, there are Sources (which represent an  
entire web site) and Resources (which map to a single URL). Each  
Source has\_many Resources (so each Resource belongs\_to a Source).

In my action, I need to create both a Source and a linked Resource.  
However, each has some validation that needs to occur. If either the  
Source or Resource has a problem, neither should be created.

My initial thought was to use a transaction. I would use save! so that  
if anything went wrong, I could just print out the error in the flash.  
My controller action contained code that looked something like this:

begin  
Source.transaction do  
@resource =Resource.new(:source =\> Source.new(:name =\>  
params[:name]), :url =\> params[:url])  
@source = @resource.source  
@source.save!  
@resource.save!  
end

# do some stuff

rescue ActiveRecord::RecordInvalid, AssertionFailure =\> error  
# put the error message in the flash  
render :action =\> ‘new’  
end

If there is something wrong in Source (e.g. the name is empty), this  
works fine - it prints out the nice error message I have in the Source  
model.

However, if something is wrong in the Resource, Source just gives the  
following error: ‘Validation failed: Resources is invalid’. That’s not  
very helpful to the user.

Of course, I can do @resource.save! first, but if there is a problem  
with the Source, ActiveRecord complains that the source\_id cannot be  
null (since it could not save).

Clearly my approach is very wrong here. My goal is to

1. Only save the Source and Resource if both are valid
2. Not duplicate a bunch of validation logic in the controller

Anyone have ideas for how I can accomplish this?

Thanks in advance!

Ben

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 1:57am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/2 "2007-12-07T01:57:47Z")

</div>

For this:

@resource =Resource.new(:source =\> Source.new(:name =\>  
params[:name]), :url =\> params[:url])  
@source = @resource.source  
@source.save!  
@resource.save!

You don’t need these lines:

@source = @resource.source  
@source.save!

You could alternatively take Source.new out from Resource.new and make  
it:

@source = Source.new(:name =\> params[:name])

and then  
@resource = Resource.new(:source =\> @source)

As source is saved along with the resource.

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 2:55am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/3 "2007-12-07T02:55:06Z")

</div>

Ryan,

You’re right, there is definitely some unnecessary code in there.  
However, even if I simplify it as you suggested, there is still the  
problem that doing @source.save! gives the following exception

‘Validation failed: Resources is invalid’.

which isn’t very useful. I’d like it to give the actual validation  
failure in the Resource … something like ‘URL is not valid’. Am I  
totally approaching this problem the wrong way? It feels like I’m  
working way too hard to do something reasonably simple - which usually  
means I’m doing something pretty boneheaded :).

Ben

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 2:59am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/4 "2007-12-07T02:59:46Z")

</div>

Ryan,

You’re right, there is definitely some unnecessary code in there.  
However, even if I simplify it as you suggested, there is still the  
problem that doing @source.save! gives the following exception

‘Validation failed: Resources is invalid’.

which isn’t very useful. I’d like it to give the actual validation  
failure in the Resource … something like ‘URL is not valid’. Am I  
totally approaching this problem the wrong way? It feels like I’m  
working way too hard to do something reasonably simple - which usually  
means I’m doing something pretty boneheaded :).

Ben

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 4:31am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/5 "2007-12-07T04:31:33Z")

</div>

Whoops, it looks like my browser did something funky. Sorry about  
that.

If the Source has a validation error, the problem with calling  
@resource.save! is that it throws an exception saying it cannot write  
the resource to DB because source\_id is null. Which makes sense -  
since the Source is not valid, it won’t write to the DB and therefore  
does not have a valid ID.

Hopefully this one will not double post…

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 3:51am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/6 "2007-12-07T03:51:49Z")

</div>

Any reason for the double post?

If you want to give the error of the resource then calling  
@resource.save!  
should return it. Never call @source.save!

On Dec 7, 2007 12:29 PM, bhbrinckerhoff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> failure in the Resource … something like ‘URL is not valid’. Am I
> 
> > @resource =Resource.new(:source =\> Source.new(:name =\>  
> > You could alternatively take Source.new out from Resource.new and make  
> > Ryan B.

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 4:44am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/7 "2007-12-07T04:44:26Z")

</div>

In that case:

(assuming resource has many sources)

@resource = Resource.new  
@resource.sources.build = Source.new(:name =\> “Source”)  
@resource.save!

Should work.

If I got it the wrong way around just swap source with resource and you  
should get the idea.

On Dec 7, 2007 2:00 PM, bhbrinckerhoff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hopefully this one will not double post…
> 
> > > ‘Validation failed: Resources is invalid’.
> > > 
> > > > For this:  
> > > > @source.save!  
> > > > As source is saved along with the resource.
> > 
> > > > –  
> > > > Ryan B.
> > 
> > –  
> > Ryan B.

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 5:14am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/8 "2007-12-07T05:14:50Z")

</div>

Ryan,

Hey thanks for the tip. It’s not working in my example quite yet  
(probably due to some other factors I didn’t include in my post), but  
it looks promising. Unfortunately, I’m leaving for a trip tomorrow, so  
I may not get to work on it until Monday. In any case, I’ll report  
back with what I find.

Thanks!  
Ben

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 7, 2007, 4:46am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/9 "2007-12-07T04:46:11Z")

</div>

Whoops,

that’s @resources.sources.build(:name =\> “Source”)

A bit too eager to hit the send button.

On Dec 7, 2007 2:14 PM, Ryan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I got it the wrong way around just swap source with resource and you
> 
> > @resource.save! is that it throws an exception saying it cannot write  
> > resource.save!
> > 
> > > 
> > 
> > usually
> > 
> > > > > @source.save!
> > > 
> > > –  
> > > Ryan B.
> > > 
> > > >
> 
> –  
> Ryan B.

–  
Ryan B.

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 8, 2007, 12:19am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/10 "2007-12-08T00:19:37Z")

</div>

About the Source being really invalid but the user getting errors for  
the Resource.source\_id also, you could do

instead of @resource.save!  
use @resource.save! if @source.valid?

---

<div class="post-metadata">

**Author:** ![bhbrinckerhoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@bhbrinckerhoff](https://www.ruby-forum.com/u/bhbrinckerhoff)\
**Post date:** [December 8, 2007, 12:16am UTC](https://www.ruby-forum.com/t/fat-models-and-validation/122371/11 "2007-12-08T00:16:41Z")

</div>

Apparently your problem is with telling the user what the problem is,  
right?

Wouldn’t adding \<%= error\_messages\_for ‘source’ %\> and \<%=  
error\_messages\_for ‘resource’ %\> in the view help you with that?
