Hi there, I think it should be easy, but I can''t find a decent way to execute a database function which I''ve declared in my database-schemas in postgres. I have a function like CREATE FUNCTION rating_param_is_leaf(INTEGER) RETURNS BOOLEAN AS $$ SELECT count(*) = 0 FROM _rating_params WHERE superparam_id = $1; $$ LANGUAGE SQL; I''d like to make a method in the "rating_param" model, which directly returns the result of the "rating_param_is_leaf" function. How do I go about that? I have several function of which I want this. Thanks in advance. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Wiebe Cazemier wrote:> I''d like to make a method in the "rating_param" model, which directly > returns the result of the "rating_param_is_leaf" function. How do I go > about that? >I don''t know if this will work but I don''t see a reason why not: def rating_param(p) r = find_by_sql("SELECT rating_param_is_leaf(#{p.to_i})").at(0) r.rating_param_is_leaf end -- Kamil
Kamil Kukura wrote:> > I don''t know if this will work but I don''t see a reason why not: > > def rating_param(p) > r = find_by_sql("SELECT rating_param_is_leaf(#{p.to_i})").at(0) > r.rating_param_is_leaf > endI changed it into: def is_leaf r = RatingParam.find_by_sql("SELECT rating_param_is_leaf(#{id})").at(0) r.rating_param_is_leaf end I still don''t think it''s perfect, but I guess it works. Thanks. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 8, 2005, at 4:22 AM, Wiebe Cazemier wrote:> def is_leaf > r = RatingParam.find_by_sql("SELECT rating_param_is_leaf(# > {id})").at(0) > r.rating_param_is_leaf > end > > I still don''t think it''s perfect, but I guess it works. Thanks.def leaf? !self.class.connection.select_value("SELECT rating_param_is_leaf (#{quoted_id})").to_i.zero? end jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDcKwWAQHALep9HFYRAkiZAJ9DO9nre8xj3DzWGFta3uQQvIysRQCdFeMM W+Rv0Ouumya1OfFgaAqDiyg=3sY9 -----END PGP SIGNATURE-----