Add some features to master table class on ActiveRecord.
Add this line to your application's Gemfile:
gem 'mastar'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mastar
include Mastar
in your ActiveRecord classes.mastar.key
method.class Country < ActiveRecord::Base
include Mastar
mastar.key :code
end
class City < ActiveRecord::Base
belongs_to :country
end
id | name | code |
---|---|---|
1 | USA | us |
2 | Japan | jp |
Country.jp
returns #<Country id: 2, name: "Japan", code: "jp", ...>
Country.jp(:name)
returns "Japan"
Country.jp(:code, :name, :id)
returns ["jp", "Japan", 2]
City.first.country.jp?
).get
method like as .find
, .get
preferentially returns cached object.
Country.find
returns from DB.Country.get
returns from inner cache when already cached.Country.get
returns from DB, and cache it..pairs
method for writing select element shortly in Rails.# Normal
f.select :country_id, Country.all.map { |c| [c.name, c.id] }
f.collection_select :country_id, Country.all, :id, :name
# In using Mastar
f.select :country_id, Country.pairs
git checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)