Forum: Ruby on Rails Re: Execute database function from model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
kamil.kukura (Guest)
on 2005-11-13 16:56
(Received via mailing list)
Wiebe C. 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
halfgaar (Guest)
on 2005-11-13 16:56
(Received via mailing list)
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
> end

I 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.
jeremy (Guest)
on 2005-11-13 16:56
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 8, 2005, at 4:22 AM, Wiebe C. 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-----
This topic is locked and can not be replied to.