diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-10-01 19:15:51 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-10-01 19:15:51 +0000 |
commit | 0092d0ac6de041995d6f4555c6e52590799cbefb (patch) | |
tree | ee9c20184991e1226c79430c9527fa9086ad9757 /activerecord/lib | |
parent | f1d1f01b6d792b6e02bde2b5d73f833edc65d211 (diff) | |
download | rails-0092d0ac6de041995d6f4555c6e52590799cbefb.tar.gz rails-0092d0ac6de041995d6f4555c6e52590799cbefb.tar.bz2 rails-0092d0ac6de041995d6f4555c6e52590799cbefb.zip |
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
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 7 |
1 files changed, 6 insertions, 1 deletions
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 |