i tried to use the "method_missing" for my problem.
i created the following:
table "group_options" with key:string
it contains every option like "create_events_roles" as the key
table "group_options_relations" with group_option_id:integer
role_id:integer group_id:integer
it is the join table for the role and the group_option
i added the following method to my group model:
def method_missing(m, *args, &block)
@option = GroupOption.find_by_key(m.to_s) ||
GroupOption.find_by_key(m.to_s.match(/(\w*)\?=/)[1])
if @option
if args.size != 0
GroupOptionsRelation.find_all_by_group_option_id_and_group_id(@
option.id,
self.id).each do |go|
go.destroy
end
args.first.each do |a|
GroupOptionsRelation.create(:group_option_id =>
@
option.id, :role_id => a.to_i, :group_id =>
self.id)
end
else
return
GroupOptionsRelation.find_all_by_group_option_id_and_group_id(@
option.id,
self.id)
end
else
super
end
end
it "does" something that i "could" use. but it doesn't "feel" right...
?> g = Group.find(4)
=> #<Group id: 4, title: "Test Group">
>> g.create_events_roles
=> [#<GroupOptionsRelation id: 2, role_id: 5, group_option_id: 1,
created_at: "2008-07-15 00:15:15", updated_at: "2008-07-15 00:15:15",
group_id: 4>, #<GroupOptionsRelation id: 3, role_id: 12,
group_option_id: 1, created_at: "2008-07-15 00:15:15", updated_at:
"2008-07-15 00:15:15", group_id: 4>, #<GroupOptionsRelation id: 4,
role_id: 19, group_option_id: 1, created_at: "2008-07-15 00:15:15",
updated_at: "2008-07-15 00:15:15", group_id: 4>]
>> g.create_events_roles [4,2]
=> [4, 2]
>> g.create_events_roles
=> [#<GroupOptionsRelation id: 5, role_id: 4, group_option_id: 1,
created_at: "2008-07-15 00:22:20", updated_at: "2008-07-15 00:22:20",
group_id: 4>, #<GroupOptionsRelation id: 6, role_id: 2,
group_option_id: 1, created_at: "2008-07-15 00:22:20", updated_at:
"2008-07-15 00:22:20", group_id: 4>]
hmmmm
any ideas how to clean this up? Go a "better" way?
thanks in advance,
Pascal