我的lib目录中有一个名为query.rb的脚本。我想要它,所以我可以从Rails控制台运行它。我想知道这是否可行。该脚本在应用程序中运行良好,因此我知道它的结构良好且功能齐全。
答案 0 :(得分:4)
对于Rails 3+,请使用load "#{Rails.root}/lib/your_lib_file.rb"
加载工作类似于require,但允许您在编辑文件时重新加载文件(与require不同,您无法再次运行)。见https://stackoverflow.com/a/6361502/513739
答案 1 :(得分:2)
require "#{RAILS_ROOT}/lib/query"
答案 2 :(得分:1)
>> $:.unshift 'lib'
>> require 'query'
答案 3 :(得分:1)
对于Rails 4 +:
DROP FUNCTION IF EXISTS leaflet_upsert_usercomments(int[], text[]);
-- Returns a set of op,cartodb_id values where op means:
-- deleted: -1
-- updated: 0
-- inserted: 1
CREATE OR REPLACE FUNCTION leaflet_upsert_usercomments(
cartodb_ids integer[],
geojsons text[])
RETURNS TABLE(op int, cartodb_id int)
LANGUAGE plpgsql SECURITY DEFINER
RETURNS NULL ON NULL INPUT
AS $$
DECLARE
sql text;
BEGIN
sql := 'WITH n(cartodb_id,the_geom) AS (VALUES ';
-- Iterate over the values
FOR i in 1 .. array_upper(geojsons, 1)
LOOP
IF i > 1 THEN sql := sql || ','; END IF;
sql :=sql || '('||cartodb_ids[i]||','
|| 'ST_SetSRID(ST_GeomFromGeoJSON(NULLIF('''|| geojsons[i] ||''','''')),4326))';
END LOOP;
sql := sql || '), do_update AS ('
|| 'UPDATE leaflet_data p '
|| 'SET the_geom=n.the_geom FROM n WHERE p.cartodb_id = n.cartodb_id '
|| 'AND n.the_geom IS NOT NULL '
|| 'RETURNING p.cartodb_id ), do_delete AS ('
|| 'DELETE FROM leaflet_data p WHERE p.cartodb_id IN ('
|| 'SELECT n.cartodb_id FROM n WHERE cartodb_id >= 0 AND '
|| ' n.the_geom IS NULL ) RETURNING p.cartodb_id ), do_insert AS ('
|| 'INSERT INTO leaflet_data (the_geom)'
|| 'SELECT n.the_geom FROM n WHERE n.cartodb_id < 0 AND '
|| ' n.the_geom IS NOT NULL RETURNING cartodb_id ) '
|| 'SELECT 0,cartodb_id FROM do_update UNION ALL '
|| 'SELECT 1,cartodb_id FROM do_insert UNION ALL '
|| 'SELECT -1,cartodb_id FROM do_delete';
RAISE DEBUG '%', sql;
RETURN QUERY EXECUTE sql;
END;
$$;
-- Grant access to the public user
GRANT EXECUTE ON FUNCTION leaflet_upsert_usercomments(integer[],text[]) TO publicuser;