assignable_values 0.9.0 supports virtual attributes
Now you can say:
class Song < ActiveRecord::Base
attr_accessor :virtual_attribute
assignable_values_for :virtual_attribute do
%w[air fluff]
end
end
Related cards:
Auto-coerced virtual attributes with Virtus
We've since created ActiveType which has a restricted subset of Virtus' features. It might be enough for your needs.
We sometimes give our ActiveRecord models [virtual attributes](http://railscasts.c...
Virtual attributes for integer fields
Note that this card is very old. You might want to use ActiveType for your auto-coerced virtual attributes instead.
We sometimes give our models [virtual att...
Virtual attributes for date fields
Note that this card is very old. You might want to use ActiveType for your auto-coerced virtual attributes instead.
We sometimes give our models [virtual att...
Rails: Using database default values for boolean attributes
In the past we validate and set default values for boolean attributes in Rails and not the database itself.
Reasons for this:
- Older Rails didn't s...
assignable_values 0.11.0 can return *intended* assignable values
As you know, assignable_values does not invalidate a record even when an attribute value becomes unassignable. See this example about songs:
class Song < ActiveRecord::Base
belongs_to :artist
belongs_to :record_label
assign...
Virtual attributes for array fields
When a has_many
association basically serves to store a list of associated strings (tags, categories, ...), it can be convenient to represent this association as a string array in the containing model. Here is an example for this pattern from th...
CSS: Matching against attributes and their values (or parts of them)
You probably know that you can use CSS selectors to match against elements and their attributes, such as:
a[title] { /* any <a> that has a "title" */ }
a[data-fancy="true"] { /* any <a> that has their "data-fancy" attribute set to "true" *...
Angular: Keeping attributes with invalid values in an ngModel
The Angular 1.2 way:
# By default, angular returns undefined for invalid attributes which removes
# the value from the form field's ngModel (which means it's not sent to the
# server, and old values would not be overwritten).
#
# This directi...
Don't assign time values to date attributes
Do not pass times to date attributes. Always convert times to dates when your application uses time zones.
Background
A time-zoned Time
attribute on a Rails record is converted to UTC using to_s(:db)
to be stored, and converted back into t...
Updated: assignable_values can now humanize any given value, even if it's not the current attribute
You've always been able to access the humanized version for the current value like this:
song = Song.new(:genre => 'pop')
song.humanized_genre # => 'Pop music'
You can now also retrieve the humanized version of any given value by passing...