I have an action that needs to update two models/two tables in one shot,
and it works great, but ignores one column. Here is the action code:
(verify_id is irrelevant in this case)
def edit
@page_title = "Edit User"
@page_active_3 = "active"
if request.post?
@user = User.find(params[:id])
@profile = @user.profile
verify_id(@user.id, true)
begin
if @user.update_attributes(params[:user]) and
@profile.update_attributes(params[:profile])
flash[:notice] = 'User was successfully updated.'
redirect_to :action => 'view', :id => @user.id
else
render :action => 'edit'
end
rescue
flash[:notice] = 'Errors occurred when attempting to update,
contact admin or check production.log'
redirect_to :action => 'view', :id => @user.id
end
else # if GET request
@user = User.find(params[:id])
@profile = @user.profile
verify_id(@user.id, true)
end
end
here is the view code:
<%= error_messages_for 'user' %>
<%= error_messages_for 'profile' %>
<div class="div_form">
<% if session[:admin] == 'yes' -%>
<h2>Login Details</h2>
<p><label for="user_admin">Administrator Access</label>
<%= select 'user', 'admin', [ ['no', 'no'],
['yes', 'yes'] ] %></p>
<% end %>
<h2>Personal Profile</h2>
<p><label for="profile_first_name">First Name</label><br />
<%= text_field 'profile', 'first_name', :class => 'text_medium' %></p>
<p><label for="profile_last_name">Last Name</label><br />
<%= text_field 'profile', 'last_name', :class => 'text_medium' %></p>
<p><label for="profile_location">UY Location</label>
<%= select 'profile', 'location', [ ['USA', 'USA'],
['South Africa', 'South Africa'],
['UK', 'UK'],
['South America', 'South America'],
['Europe', 'Europe'],
['Asia', 'Asia'] ] %>
</p>
<p><label for="profile_email">Email Address</label><br />
<%= text_field 'profile', 'email', :class => 'text_medium' %></p>
</div>
I suspect it may be a model validation problem, but i’ve removed all
validations for the edit/update action, but here is the model
require "digest/sha1"
class User < ActiveRecord::Base
# Validations
validates_uniqueness_of :login_name,
:message => 'has already been taken',
:on => :save, :on => :create
validates_presence_of :login_name, :password, :admin,
:message => 'is required',
:on => :save, :on => :create
validates_associated :profile,
:on => :save, :one => :create
# Associations
belongs_to :profile, :foreign_key => 'profile_id',
:dependent => true
has_many :articles
# Attributes
attr_accessor :password
attr_accessible :login_name, :password
.... etc ....