Hi everyone,
I am having the following problem: I have a ''User'' model and
a
''User_option'' model (user has_many user_options &
user_options
belongs_to user).
Now the user_option table (and model) consist mostly of a key & value
fields, and I would like to be able to reference them like an
associative array
instead of a collection. Here is what I mean:
user = User.find(:first)
user.user_options[:key]
instead of having to do:
user.user_options.find {|option| option.key==key}
Someone suggested I should do it like this:
class User < ActiveRecord::Base
has_many :user_options do
def [](key)
user_options.find {|opt| opt.key==key} #(search code)
end
end
end
The problem is the user_options array is not visible from within the
method because the method is in effect
inside the user_option class(model). So I am wondering how I can
reference the ''outter'' class User from within the
method ''def [](key)''. Is this possible? The purpose of
reusing the
user_options array is that I don''t have to deal directly with the
database.
Any help would be greatly appreciated...or any other suggestion on a way
to accomplish the ''user.user_options[:key]'' thing.
Thank you,
Jd