Validation issue with HABTM
Reported by flip | July 17th, 2008 @ 06:38 PM | in 2.x
Validation of attributes by the model layer is not always possible with a HABTM relationships.
Here is a simple situation so you can understand where i'm going :
Condition Model
class Condition < ActiveRecord::Base
has_and_belongs_to_many :departments, :order => 'numero ASC'
validates_presence_of :departments
end
Departments Model
class Department < ActiveRecord::Base
has_and_belongs_to_many :conditions
end
Condition Controller
class ConditionController < ApplicationController
def edit
params[:condition] ||= {}
params[:condition][:department_ids] ||= []
@condition = Condition.find_by_id(params[:id])
if request.post? and @condition.update_attributes(params[:condition])
flash[:notice] = "Updated successfully"
redirect_to :action => :list
end
end
end
Let's assume the tables and the join table (conditions_departments)are created.
Well the thing is : we want to ensure there is a least one department associated with a condition. On creation, there is no problem, if departments is empty the condtion object is not created
department = Department.new.save
condition = Condition.new(:department_ids => [])
condition.save
=> false
condition.departments<<department
condition.save
=>true
However on update, (with update_attributes), if the validation fails on departments the condition object is not updated, but the join table conditions_departments is updated with the invalid data.
condition.department_ids = []
condition.valid?
=>false
condition.departments.empty?
=>true
The thing is that whenever you do something like :
condition.department_ids = something
condition.departments = something
It gets saved instantly on the database, and update_attributes uses this kind of assignation method. So even if there is an error on the association it gets saved.
To ensure we don't save invalid data on the database we've got to make some validation on the contoller layer : testing directly the data or calling a validation method which ensure the data in the params hash is correct.
Ok but if we follow the MVC architecture the validation has to stay to the model layer.
If the collection of an HABTM association doesn't gets instantly saved on modifications it'd be great, and so convenient. I still don't understand why this choice of instant save has been made
Comments and changes to this ticket
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
The latest development for the 1.2.x and 2.0.x releases are on the 1-2-stable and 2-0-stable branches.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
