-
Notifications
You must be signed in to change notification settings - Fork 9
Data Access
Andrew Geweke edited this page Dec 11, 2013
·
2 revisions
Every field you define on a flex column can be accessed in (potentially) several ways. As an example:
class User < ActiveRecord::Base
flex_column :user_attributes do
field :foo
field :bar
end
end
my_user = User.find(...)
From always-defined to not-always-defined:
-
Hash indexing from inside the flex-column object: always works. Custom methods you define on the flex-column object can always access fields by saying
self[:foo]
,self[:foo] = 123
, and so on. Hash-indexing is done as a HashWithIndifferentAccess, so you can use a String or a Symbol, or mix the two. Note that any attributes you haven't declared fields for won't be accessible, even this way; see unknown JSON keys for more details. -
Hash indexing from outside the flex-column object: always works.
my_user.user_attributes[:foo] = 123
,x = my_user.user_attributes['foo']
, and so on. -
Flex-column methods from inside the flex-column object: always works, unless you override them. Custom methods you define on the flex-column object can access fields by simply calling a method of the same name:
self.foo = 123
,self.foo
, and so on. -
Flex-columns methods from outside the flex-column object: works unless you make them private. For example,
my_user.user_attributes.foo = 123
-
Model methods from inside the model object: works unless (a) you turn off delegation, (b) your model has a column of the same name as the field in question, or (c) another flex column, declared afterwards (it's "last declaration wins"), has a field of the same name. For example, from a method on
User
,self.foo = 123
,x = foo
, and so on. -
Model methods from outside the model object: works unless (a) you turn off delegation, (b) your model has a column of the same name as the field in question, (c) another flex column, declared afterwards (it's "last declaration wins"), has a field of the same name, or (d) you make the methods private. For example,
my_user.foo = 123
,x = my_user.foo
, and so on. -
Methods included into another class (using
include_flex_columns_from
: works only if you include this flex column into another model class usinginclude_flex_columns_from
, and then will not work if (a) you turn off delegation, (b) your model has a column of the same name as the field in question, (c) another flex column, declared afterwards (it's "last declaration wins"), has a field of the same name, or (d) you make the methods private. For example, assuming some modelAdmin
that declareshas_one :user
,my_admin.foo = 123
,x = my_admin.foo
, and so on.