ActiveRecord::Denormalize
Denormalize fields in ActiveRecord.
Installation
Add this line to your application's Gemfile:
gem 'activerecord-denormalize'
And then execute:
$ bundle
Or install it yourself as:
$ gem install activerecord-denormalize
Usage
class Message < ActiveRecord::Base
# sender_type:string, sender_id:integer, _sender:hstore
belongs_to :sender, polymorphic: true
end
class User < ActiveRecord::Base
# name:string
has_many :messages, as: :sender, denormalize: {
attributes: [:name]
}
end
user = User.create!(name: 'foo')
= Message.create!(sender: user)
.sender_name #=> 'foo'
user.update_attribute :name, 'bar'
.reload
.sender_name #=> 'bar'
Message.all.each do |msg|
# no N+1 query
msg.sender_name
end
Running the test
$ bundle exec rake appraisal spec
Contributing
- Fork it
- 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