Acts_as_enumerated

Hello All,

Any one using acts_as_enumerated? I need help using (I like that it
caches values in memory)

I am working on a dating website and there are lots of options I want to
store as enumerated like

Status; divorce, single,
Sex: male, female
Eye color; blue, brown, green…
and lots more…

But I do not wanna keep them in seperate tables, and wanna keep them all
together.

Anyone has a working example virtual_enumerations.rb file for
acts_as_enumarated?

Thanks
Gokhan

virtual_enumerations.rb

ActiveRecord::VirtualEnumerations.define do |config|

config.define ‘Enumeration’,
:order => ‘position ASC’

config.define ‘Status’,
:extends => ‘Enumeration’

config.define ‘Sex’,
:extends => ‘Enumeration’

config.define ‘Eye’,
:extends => ‘Enumeration’
end

Schema

create_table “enumerations”, :force => true do |t|
t.column “enumeration_type”, :string
t.column “name”, :string
t.column “description”, :string
t.column “position”, :integer
t.column “migration_code”, :string
t.column “created_at”, :time
t.column “updated_at”, :time
end

Migration for User

create_table :users do |t|
t.column “gender_id”, :integer
t.column “status_id”, :integer
t.column “eye_id”, :integer
end

Would allow @user.gender = Gender[:m]

or even more readable would be @user.gender = Gender[:male]

regards,
Leon L…

Leon L. wrote:

Schema

create_table “enumerations”, :force => true do |t|
t.column “enumeration_type”, :string
t.column “name”, :string
t.column “description”, :string
t.column “position”, :integer
t.column “migration_code”, :string
t.column “created_at”, :time
t.column “updated_at”, :time
end

One more question;

ActiveRecord::VirtualEnumerations.define do |config|
config.define ‘Enum’,
:order => ‘position ASC’

config.define ‘Status’,
:extends => ‘Enum’

config.define ‘Gender’,
:extends => ‘Enum’

config.define ‘Eye’,
:extends => ‘Enum’
end

and in my User model
has_enumerated :gender, :class_name => ‘Enum’

And in the migration
create_table :enums do |t|
t.column “enumeration_type”, :string
t.column “name”, :string
t.column “description”, :string
t.column “position”, :integer
t.column “migration_code”, :string
t.column “created_at”, :datetime
t.column “updated_at”, :datetime
end

Enum.create(:enumeration_type => "Gender", :name=>"m", :position=> 

“1”)
Enum.create(:enumeration_type => “Gender”, :name=>“f”, :position=>
“2”)

In console
$ ./script/console
Loading development environment.

@user= User.find(:first)
=> #<User:0x274fb70 @attributes={“city”=>nil, “updated_at”=>“2006-06-18
00:53:27”, “zip”=>nil,
“activation_code”=>“94b91ef6b693b5d5b38e882ef96b83f3a8b14eac”,
“enabled”=>“1”, “gender”=>“m”, “id”=>“1”, “activated”=>“1”,
“mobile”=>nil, “region_id”=>nil, “birthday”=>nil, “phone”=>nil,
“accessed_at”=>nil, “message_alert”=>“0”, “first_name”=>“Gokhan”,
“state_id”=>nil, “address”=>nil, “last_name”=>“Arli”,
“password”=>“b906314abb8dfd93812559e47ba668c0375ba9c3”, “login”=>“g”,
“created_at”=>“2006-06-17 23:17:52”, “email”=>“[email protected]”}>

@user.gender = Gender[:m]
NameError: uninitialized constant Gender
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
const_missing' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:inconst_missing’
from (irb):2
from :0

Why do I get this error message? I forget something somewhere?

Thanks
Gokhan

Hey,

info inserted below:

On 18-Jun-06, at 2:41 PM, Gokhan A. wrote:

t.column "updated_at", :time

config.define ‘Gender’,
:extends => ‘Enum’

config.define ‘Eye’,
:extends => ‘Enum’
end

Okay, you are using STI when you say :extends => ‘Enum’, which means
that (be default) you need a ‘type’ column in your table. However,
it seems you’ve got an ‘enumeration_type’ column in your migration
which is doing that job right?

In that case you need to define the ‘Enum’ virtual enumeration like
this:

ActiveRecord::VirtualEnumerations.define do |config|

config.define ‘Enum’, :order => ‘position ASC’ do
set_inheritance_column ‘enumeration_type’
end

the rest of your virtual enums …

end

more below:

and in my User model
has_enumerated :gender, :class_name => ‘Enum’

DON’T say :class_name => ‘Enum’. If you do that it will allow
assign any subclass of Enum to the :gender attribute and I doubt
that’s what you want (i.e. foo.gender = Status[:open]).

You should be able to just say:

has_enumerated :gender

Or, if you want to be ultra-verbose about it:

has_enumerated :gender, :class_name => ‘Gender’

more below:

end

Enum.create(:enumeration_type => "Gender", :name=>"m", :position=>

“1”)
Enum.create(:enumeration_type => “Gender”, :name=>“f”, :position=>
“2”)

Hrm… did this migration actually run? Because of the caching I try
to protect myself from my own stupidity by demanding extra steps
before allowing any alterations to aae model data.

Gender.enumeration_model_updates_permitted = true
Gender.create(:name => ‘m’, :position = 1)
Gender.create(:name => ‘f’, :position = 2)

The enumeration_model_updates_permitted thing is long and unruly to
type and is there as a way of saying “yes dammit, I know what I’m
doing, just update the damn data okay?” :slight_smile:

However, you’ve got a more important issue and none of this will
work till you fix it. Se below:

“mobile”=>nil, “region_id”=>nil, “birthday”=>nil, “phone”=>nil,
from

That stacktrace is all wrong - as in the virtual_enumerations code
hasn’t loaded (and I think there’s a good reason for that). Here’s
what my stacktrace looks like when it can’t find a class:

NameError: uninitialized constant Gender
from ./script/…/config/…/config/…/vendor/rails/
activerecord/lib/…/…/activesupport/lib/active_support/
dependencies.rb:123:in enumerations_original_const_missing' from ./script/../config/../config/../vendor/plugins/ enumerations_mixin/lib/active_record/virtual_enumerations.rb:63:inconst_missing’
from ./script/…/config/…/config/…/vendor/rails/
activerecord/lib/…/…/activesupport/lib/active_support/
dependencies.rb:131:in `const_missing’
from (irb):2

Notice that the first ‘from’ line in my stacktrace complains about
the ‘enumerations_original_const_missing’ method while yours just
says ‘const_missing’.

So, assuming that the plugin has loaded (and you don’t get complaints
about ‘has_enumerated’ being undefined when you load your User model)
then it’s going to be the fact that the plugin didn’t see your
virtual_enumerations.rb file (the plugin won’t patch the rails class
loading code if that file isn’t found).

Make sure your virtual_enumerations.rb file is in the config
directory (the same one as your database.yml file).

Let me know how you get on with this - I’ll be out of contact from
Monday morning until Wednesday afternoon though so if I don’t
respond … that’s why.

Regards,
Trevor

Hey Leon

(apart from being really chuffed that AAE has proved useful to other
people) I was wondering, what’s the migration_code column for?

Trev

Trevor S.
http://somethinglearned.com

Hi Trevor.

Let me first say that migration_code has nothing to do with Active
Record
migrations. I used it to convert a legacy system lookups/keywords to
enumerations. Old code from the legacy system are stored in
migration_code.

And AAE has been useful to me (Thanks). I sometimes wonder, how are
others
able to undertake large projects( > 100 models) without it.

regards,
Leon

Leon L. wrote:

Hi Trevor.

Let me first say that migration_code has nothing to do with Active
Record
migrations. I used it to convert a legacy system lookups/keywords to
enumerations. Old code from the legacy system are stored in
migration_code.

And AAE has been useful to me (Thanks). I sometimes wonder, how are
others
able to undertake large projects( > 100 models) without it.

regards,
Leon

Thanks to both of you,

I copied configuration into my environments and it works now, I am not
sure why it did not work initially, I will come back to that later.

Thanks again
Gokhan

Hi Guys

resurrecting this old chestnut again…

I want to use this plugin for a many-many relationship with a join
table, with one of the models being enumerated… does it still hold the
same benefits with respect to caching??

also, if the size of the table for the enumerated model is very large
(though still a defineable set), e.g. regions of each country of the
world, is it still advantageous to use this system?

regards

Paul

Paul N. wrote:

I want to use this plugin for a many-many relationship with a join
table, with one of the models being enumerated… does it still hold the
same benefits with respect to caching??

Sure. All instances will have their attributes cached, but the
association will also be cached. So you’ll have to reload an
association if you want the latest data.

also, if the size of the table for the enumerated model is very large
(though still a defineable set), e.g. regions of each country of the
world, is it still advantageous to use this system?

Well, it’s an easy and fast way if you think the memory usage is
worth it. But if it’s taking too much memory for the benefit
gained, you may instead wish to use a limited-size LRU cache.


Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com