Sha256: 97d80112f5c55b4dbdbc77aebbcf10df6df3cc3f3729034996a38845480e9750

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# activerecord_lookup_or_initialize [![CircleCI](https://circleci.com/gh/vitalinfo/activerecord_lookup_or_initialize.svg?style=svg)](https://circleci.com/gh/vitalinfo/activerecord_lookup_or_initialize)

ActiveRecord::Relation extension.
Search object inside association. 


Method `find_or_initialize_by` return object outside collection, to get back inside use `lookup_or_initialize_by` instead.


## Installation

Add this line to your application's Gemfile:

```ruby
gem 'activerecord_lookup_or_initialize'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install activerecord_lookup_or_initialize

## Usage
```ruby
class User < ActiveRecord::Base
  has_many :comments, dependent: :destroy, inverse_of: :user
end

class Comment < ActiveRecord::Base
  belongs_to :user, inverse_of: :comments
end

user = User.first
comment = user.comments.lookup_or_initialize_by(message: 'first user message')
```

## Contributing
1. Fork it
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create new Pull Request

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord_lookup_or_initialize-0.1.0 README.md