# Problems with multiple ActiveRecords and Validations

**URL:** <https://www.ruby-forum.com/t/problems-with-multiple-activerecords-and-validations/57426>\
**Category:** Rails\
**Created:** [April 10, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/problems-with-multiple-activerecords-and-validations/57426 "2006-04-10T21:20:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dominik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@Dominik](https://www.ruby-forum.com/u/Dominik)\
**Post date:** [April 10, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/problems-with-multiple-activerecords-and-validations/57426/1 "2006-04-10T21:20:28Z")

</div>

Hi all,

I am a RoR newbie and have the following scenario set up:

There are 2 ActiveRecords involved, the first one being “User”, the  
second one being “Visibility”.

The corresponding users table holds address informations regarding a  
User. The visibilities table holds visibility settings for the  
individual columns of the users table (i.e. firstname, lastname, country  
should be visible to everybody while the credit card data should be only  
visible to the admin, and so on) for individual users. It has  
practically the same schema as the users table and a foreign key, namely  
user\_id. The relationship between the tables is 1:1.

Now I have created an “edit user profile” view which displays all the  
individual form elements corresponding to the columns of the user table.  
Next to each form element is a checkbox which is populated from the  
visibilities-table. (Un-)checking the checkbox you can decide which  
elements should be visible in the “show user profile” view. I hope you  
can follow me so far.

Everything is handled from a UserController which extracts the  
visibility alongside the user and prepares everything for display.

Problems arise, if the ActiveRecord validations come into play (e.g.  
there is a validates\_presence\_of :lastname rule). If everything  
validates successfully, the update action is invoked and all changes are  
saved (in the users and visibilities tables). But if a validation fails  
and the edit user profile view is rendered once again ONLY the submitted  
values of the User ActiveRecord are filled into the form fields once  
again. But all the checkboxes are unchecked!

I haven’t been able to figure out what exactly does happen in case a  
validation fails, although I have screened the source code of the  
ActiveRecord and ActiveView classes and come across the magic errors  
object.

Here some source code:

users\_controller.rb:

def edit  
@user = User.find(params[:id])  
@visibility = @user.visibility  
end

user.rb  
class User \< ActiveRecord::Base  
validates\_presence\_of :lastname, :firstname  
has\_one :visibility  
end

edit.rhtml (excerpt)  
\<%= text\_field ‘user’, ‘lastname’ %\>  
\<%= check\_box ‘visibility’, ‘lastname’, {}, “public”, “admin” %\>

I am desperately trying to make that work. Any ideas?

Best regards,  
Dominik

---

<div class="post-metadata">

**Author:** ![Dominik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@Dominik](https://www.ruby-forum.com/u/Dominik)\
**Post date:** [April 10, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/problems-with-multiple-activerecords-and-validations/57426/2 "2006-04-10T22:09:29Z")

</div>

Dominik wrote:

> users\_controller.rb:
> 
> def edit  
> @user = User.find(params[:id])  
> @visibility = @user.visibility  
> end
> 
> user.rb  
> class User \< ActiveRecord::Base  
> validates\_presence\_of :lastname, :firstname  
> has\_one :visibility  
> end
> 
> edit.rhtml (excerpt)  
> \<%= text\_field ‘user’, ‘lastname’ %\>  
> \<%= check\_box ‘visibility’, ‘lastname’, {}, “public”, “admin” %\>
> 
> I am desperately trying to make that work. Any ideas?
> 
> Best regards,  
> Dominik

You might want to post your “update” action from users\_controller, too.  
It’s hard to tell what happens exactly without that.

Jeff C.man

---

<div class="post-metadata">

**Author:** ![Dominik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@Dominik](https://www.ruby-forum.com/u/Dominik)\
**Post date:** [April 11, 2006, 8:49am UTC](https://www.ruby-forum.com/t/problems-with-multiple-activerecords-and-validations/57426/3 "2006-04-11T08:49:06Z")

</div>

Hi all,

I just wanted to tell you how delighted I am now: I have found the bug  
after one night’s sleep - stupid me!

It had nothing to do with the edit or update action (which I posted  
above) but with the new/create actions: the line @visibility =  
@user.visibility was missing and consequently the new action could not  
fill in the checkboxes after a failed update from the instance variable  
@visibility.

For all the newbies in the world I just want to explicitly state that  
the validations take place while update\_attributes is running. If any  
validations fail update\_attributes returns false and you can control  
what happens next with a call to render :action or redirect to :action.

Rails rocks!

---

<div class="post-metadata">

**Author:** ![Dominik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@Dominik](https://www.ruby-forum.com/u/Dominik)\
**Post date:** [April 10, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/problems-with-multiple-activerecords-and-validations/57426/4 "2006-04-10T22:41:40Z")

</div>

Jeff C.man wrote:

> Dominik wrote:
> 
> > users\_controller.rb:
> > 
> > def edit  
> > @user = User.find(params[:id])  
> > @visibility = @user.visibility  
> > end
> > 
> > user.rb  
> > class User \< ActiveRecord::Base  
> > validates\_presence\_of :lastname, :firstname  
> > has\_one :visibility  
> > end
> > 
> > edit.rhtml (excerpt)  
> > \<%= text\_field ‘user’, ‘lastname’ %\>  
> > \<%= check\_box ‘visibility’, ‘lastname’, {}, “public”, “admin” %\>
> 
> You might want to post your “update” action from users\_controller, too.  
> It’s hard to tell what happens exactly without that.

Here you are:

def update  
@user = User.find(params[:id])  
@user.create\_visibility(params[:visibility]) unless @user.visibility  
@visibility = @user.visibility  
if @user.update\_attributes(params[:user]) &&  
@user.visibility.update\_attributes(params[:visibility]) then  
flash[:notice] = ‘Update was successful.’  
redirect\_to :action =\> ‘show’, :id =\> @user  
else  
render :action =\> ‘edit’  
end  
end

And here is the relevant form-tag from the view, but that is quite  
obvious:  
\<%= start\_form\_tag :action =\> ‘update’, :id =\> @user %\>

Thanks in advance!

Dominik
