<commit_before>```ruby


class Author
  include Mongoid::Document

  has_many :books, after_add: :send_email

  def send_email(document)
    puts "Send email: #{document.inspect}"
    puts "self: #{self.inspect}"
  end
end
```


Now, the send_email callback is called with the child object whenever a book is created. Let's take a closer look.

```ruby
irb> a = Author.last
 => #<Author _id: <PASSWORD>000005, name: "after1", last_name: nil, wallet: nil, password: nil> 
> a.books.create

Send email: #<Book _id: 51b2ffbe45db7cb20d000001, t(title): nil, published_date: nil, is_best_seller: false, awards: [], reviews: nil, currency: nil, author_id: "5<PASSWORD>">

```


### inverse_of

```ruby
class Author
  include Mongoid::Document

  has_and_belongs_to_many :followers, 
                          class_name: "Author",
                          inverse_of: :following

  has_and_belongs_to_many :following, 
           class_name: "Author",
           inverse_of: :followers

end
```

```ruby
irb> a.following << b
 => [BSON::ObjectId('4ef5ab6ffed0eb89bf000001')]
When this is done, we can see that the follower_ids object of a and the following_ids of b are updated together! This is shown in the following code:


irb> a.following
 => [#<Author _id: 4ef5ab6ffed0eb89bf000001, _type: nil, name: "<NAME>", 
follower_ids: [BSON::ObjectId('4e86e4b6fed0eb0be0000011')], 
following_ids: []>]

irb> b.followers
 => [#<Author _id: 4<PASSWORD>0000011, _type: nil, name: "<NAME>", 
follower_ids: [], 
following_ids: [BSON::ObjectId('4ef5ab6ffed0eb89bf000001')]>] 

```



```ruby
class Author<commit_msg>Update chapter4_relations.markdown<commit_after>```ruby


class Author
  include Mongoid::Document

  has_many :books, after_add: :send_email

  def send_email(document)
    puts "Send email: #{document.inspect}"
    puts "self: #{self.inspect}"
  end
end
```


Now, the send_email callback is called with the child object whenever a book is created. Let's take a closer look.

```ruby
irb> a = Author.last
 => #<Author _id: 5<PASSWORD>000005, name: "after1", last_name: nil, wallet: nil, password: <PASSWORD>> 
> a.books.create

Send email: #<Book _id: 51b2ffbe45db7cb20d000001, t(title): nil, published_date: nil, is_best_seller: false, awards: [], reviews: nil, currency: nil, author_id: "5<PASSWORD>">

```


### inverse_of

```ruby
class Author
  include Mongoid::Document

  has_and_belongs_to_many :followers, 
                          class_name: "Author",
                          inverse_of: :following

  has_and_belongs_to_many :following, 
           class_name: "Author",
           inverse_of: :followers

end
```

```ruby
irb> a.following << b
 => [BSON::ObjectId('4ef5ab6ffed0eb89bf000001')]
When this is done, we can see that the follower_ids object of a and the following_ids of b are updated together! This is shown in the following code:


irb> a.following
 => [#<Author _id: 4ef5ab6ffed0eb89bf000001, _type: nil, name: "<NAME>", 
follower_ids: [BSON::ObjectId('4e86e4b6fed0eb0be0000011')], 
following_ids: []>]

irb> b.followers
 => [#<Author _id: 4e86e4b6fed0eb0be0000011, _type: nil, name: "<NAME>", 
follower_ids: [], 
following_ids: [BSON::ObjectId('4ef5ab6ffed0eb89bf000001')]>] 

```

using active record as below:

![screen shot 2015-05-18 at 9 33 40 pm](https://cloud.githubusercontent.com/assets/83296/7693868/32974eee-fda6-11e4-809a-edb907b0279f.png)


```ruby
class Author
{'fraction_non_alphanumeric': 0.1305368143150484, 'fraction_numerical': 0.062188325022000585, 'mean_word_length': 4.262345679012346, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 39, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2763446', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1200, 'n_words': 319}