From 0092d0ac6de041995d6f4555c6e52590799cbefb Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Oct 2006 19:15:51 +0000 Subject: Association collections have an _ids reader method to match the existing writer for collection_select convenience (e.g. employee.task_ids). The writer method skips blank ids so you can safely do @employee.task_ids = params[:tasks] without checking every time for an empty list or blank values. References #1887, Closes #5780. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5214 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 60f0a137b0..c73b5db51e 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -916,8 +916,13 @@ module ActiveRecord association end + define_method("#{reflection.name.to_s.singularize}_ids") do + send(reflection.name).map(&:id) + end + define_method("#{reflection.name.to_s.singularize}_ids=") do |new_value| - send("#{reflection.name}=", reflection.class_name.constantize.find(new_value)) + ids = (new_value || []).reject { |nid| nid.blank? } + send("#{reflection.name}=", reflection.class_name.constantize.find(ids)) end end -- cgit v1.2.3