rails/rails — pull request #10047 by wangjohn (2013-04-02 13:32:07+00:00)
Title: Adding a function that appends to an association only if the records do not exist

It's often the case that when you have an association and you only want to append an ActiveRecord object to the association if that association does not already hold your object. For example, consider the case where you have the following setup:

```ruby
class Blackboard
belongs_to :classroom
end

class Classroom
has_many :blackboards
end
```

Now let's say you just obtained a blackboard object from the params hash. If you want to make sure that the blackboard object is in the ``@classroom.blackboards`` association only once, you have to do a manual check to make sure you don't add it a second time.

```ruby
blackboard = Blackboard.find(params[:id])
blackboard.modify_in_some_way!

unless @classroom.blackboards.include?(blackboard)
@classroom.blackboards << blackboard
end
```

The new function I added allows you to boil the code down to:
```ruby
@classroom.blackboards.push_if_absent(blackboard)
```