<commit_before># sunspot_association [![Gem Version](https://badge.fury.io/rb/sunspot_association.png)](http://badge.fury.io/rb/sunspot_association) [![Build Status](https://secure.travis-ci.org/Arjeno/sunspot_association.png?branch=master)](http://travis-ci.org/Arjeno/sunspot_association) [![Dependency Status](https://gemnasium.com/Arjeno/sunspot_association.png)](https://gemnasium.com/Arjeno/sunspot_association) [![Coverage Status](https://coveralls.io/repos/Arjeno/sunspot_association/badge.png?branch=master)](https://coveralls.io/r/Arjeno/sunspot_association)

Automatically reindex ActiveRecord associations using Sunspot.

## Usage

Add to your Gemfile:

```ruby
gem 'sunspot_association'
```

Add to your model:

```ruby
class Company < ActiveRecord::Base
  sunspot_associate :orders, :fields => :name
end
```

## Example

```ruby
class User < ActiveRecord::Base
  belongs_to :company

  searchable do
    text :company_name do; company.try(:name) end
  end
end

class Order < ActiveRecord::Base
  belongs_to :company

  searchable do
    text :company_name do; company.try(:name) end
  end
end

class Company < ActiveRecord::Base
  has_many :orders
  has_many :users

  sunspot_associate :orders, :users
<commit_msg>Described readme with new DSL<commit_after># sunspot_association [![Gem Version](https://badge.fury.io/rb/sunspot_association.png)](http://badge.fury.io/rb/sunspot_association) [![Build Status](https://secure.travis-ci.org/Arjeno/sunspot_association.png?branch=master)](http://travis-ci.org/Arjeno/sunspot_association) [![Dependency Status](https://gemnasium.com/Arjeno/sunspot_association.png)](https://gemnasium.com/Arjeno/sunspot_association) [![Coverage Status](https://coveralls.io/repos/Arjeno/sunspot_association/badge.png?branch=master)](https://coveralls.io/r/Arjeno/sunspot_association)

Automatically reindex ActiveRecord associations using Sunspot.

## Usage

Add to your Gemfile:

```ruby
gem 'sunspot_association'
```

Add to your model:

```ruby
class Company < ActiveRecord::Base
  sunspot_associate :orders, :fields => :name
end
```

## Example

```ruby
class User < ActiveRecord::Base
  belongs_to :company

  searchable do
    associate :company, :name, :phone

    # Automatically converts to:
    # => text :company_name do; company.try(:name) end
    # => text :company_phone do; company.try(:phone) end
  end
end

class Order < ActiveRecord::Base
  belongs_to :company

  searchable do
    associate :company, :name

    # Automatically converts to:
    # => text :company_name do; company.try(:name) end
  end
end

class Company < ActiveRecord::Base
  has_many :orders
  has_many :users

  # The below configuration is automatically added by the DSL
  # => sunspot_associate :orders, :fields => :name
  # => sunspot_associate :users, :fields => [:name, :phone]

{'fraction_non_alphanumeric': 0.13397299218194741, 'fraction_numerical': 0.0, 'mean_word_length': 5.34009009009009, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 12, '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': '6129753', 'n_tokens_mistral': 965, 'n_tokens_neox': 936, 'n_words': 191}