Activerecord - 执行复杂查询

时间:2011-08-24 10:08:48

标签: sql ruby-on-rails activerecord

我想在“Activerecord”中执行以下查询。我怎样才能做到这一点?请帮帮我..

query = "SELECT rc.constraint_schema||'.'||tc.table_name AS table_name, kcu.column_name FROM information_schema.referential_constraints AS rc JOIN information_schema.table_constraints AS tc USING(constraint_catalog,constraint_schema,constraint_name) JOIN information_schema.key_column_usage AS kcu USING(constraint_catalog,constraint_schema,constraint_name) JOIN information_schema.key_column_usage AS ccu ON(ccu.constraint_catalog=rc.unique_constraint_catalog AND ccu.constraint_schema=rc.unique_constraint_schema AND ccu.constraint_name=rc.unique_constraint_name) WHERE ccu.table_catalog=\"#{connection_parameters['database']}\" AND ccu.table_schema=\"#{connection_parameters['schema_search_path']}\" AND ccu.table_name=\"#{tab}\" AND ccu.column_name=\"#{select}\""

1 个答案:

答案 0 :(得分:0)

您可以在模型方法中使用select_rows

def some_method
    query = "... big pile of SQL ..."
    connection.select_rows(sql).each do |row|
        table_name, column_name = *row
        #...
    end
end