DelegateIdTo
This gem provide two methods for access to id of has_one association.
Installation
Add this line to your application's Gemfile:
gem 'delegate_id_to'
And then execute:
$ bundle
Or install it yourself as:
$ gem install delegate_id_to
Usage
Without delegate_id_to
class Foo < ActiveRecord::Base
has_one :bar
has_one :user
delegate :id, to: :bar, allow_nil: true
delegate :id, to: :user, allow_nil: true
end
foo = Foo.last
foo. #=> nil
foo. #=> id deprecated for nil
foo.user_id #=> 42
With delegate_id_to
class Foo < ActiveRecord::Base
has_one :bar
has_one :user
delegate_id_to :bar, :user
end
foo = Foo.last
foo. #=> nil
foo. #=> nil
foo.user_id #=> 42
With delegate_id_to_singular_associations
class Foo < ActiveRecord::Base
has_one :bar
has_one :user
delegate_id_to_singular_associations
end
foo = Foo.last
foo. #=> nil
foo. #=> nil
foo.user_id #=> 42
Contributing
- Fork it ( http://github.com/shipiev/delegate_id_to/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request