Create ActiveRecord instances from a custom query?

Hi all

I’m having the following method:

def setup_owned_system_rights
@owned_system_rights = {}
system_groups.each do |system_group|
system_group.system_rights.each do |system_right|
@owned_system_rights[system_right.model_class.to_sym] = {}
unless @owned_system_rights[system_right.model_class.to_sym]
@owned_system_rights[system_right.model_class.to_sym][system_right.name.to_sym]
= true
end
end
end

To reduce the queries to the database I’ve written the following SQL
statement which returns the same system_rights that the above iterations
do:

select * from system_rights
inner join system_groups_have_system_rights on system_rights.id =
system_groups_have_system_rights.system_right_id
inner join members_belong_to_system_groups on
members_belong_to_system_groups.system_group_id =
system_groups_have_system_rights.system_group_id
where members_belong_to_system_groups.member_id = <<logged in member’s
ID>>

How can I create now ActiveRecord objects (to be more specific:
SystemRight model objects) out of the rows I get?

Thanks for help :slight_smile:

On 4 Nov 2007, at 00:49, Joshua M. wrote:

How can I create now ActiveRecord objects (to be more specific:
SystemRight model objects) out of the rows I get?

SystemRight.find_by_sql ‘some chunk of sql’

Fred