[Rails] Re: SQL search

2009-02-27 Thread Rob Biedenharn

On Feb 27, 2009, at 11:29 AM, Ga Ga wrote:
 How I write it in rails format? Articles.find( )

 SELECT *, MATCH(title, body) AGAINST('$keyword') AS score FROM  
 articles
 WHERE MATCH(title, body) AGAINST('$keyword') ORDER BY score DESC


Obviously, you'd have to try it yourself, but:

match_part = sanitize_sql(['MATCH(title, body) AGAINST(?)', keyword])
Article.find(:all, :select = *, #{match_part} AS score,
  :conditions = match_part, :order = 'score DESC')

I'm guessing that the $keyword is from Perl, but I've assumed that you  
have a local variable called keyword. Look closely at whether the  
conditions end up correct or if things get double-escaped in the final  
SQL.

-Rob

Rob Biedenharn  http://agileconsultingllc.com
r...@agileconsultingllc.com



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: SQL search

2009-02-27 Thread Ga Ga

I get:

undefined method `sanitize_sql'

-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: SQL search

2009-02-27 Thread Ga Ga

Ga Ga wrote:
 I get:
 
 undefined method `sanitize_sql'

Help ?
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: SQL search

2009-02-27 Thread Rob Biedenharn

On Feb 27, 2009, at 11:42 AM, Rob Biedenharn wrote:
 On Feb 27, 2009, at 11:29 AM, Ga Ga wrote:
 How I write it in rails format? Articles.find( )

 SELECT *, MATCH(title, body) AGAINST('$keyword') AS score FROM
 articles
 WHERE MATCH(title, body) AGAINST('$keyword') ORDER BY score DESC


 Obviously, you'd have to try it yourself, but:

 match_part =
self.class.
 sanitize_sql(['MATCH(title, body) AGAINST(?)', keyword])
 Article.find(:all, :select = *, #{match_part} AS score,
  :conditions = match_part, :order = 'score DESC')

 I'm guessing that the $keyword is from Perl, but I've assumed that you
 have a local variable called keyword. Look closely at whether the
 conditions end up correct or if things get double-escaped in the final
 SQL.

 -Rob

 Rob Biedenharnhttp://agileconsultingllc.com
 r...@agileconsultingllc.com


Sorry, if you look at the docs, sanitize_sql is a protected class  
method of ActiveRecord::Base

-Rob

Rob Biedenharn  http://agileconsultingllc.com
r...@agileconsultingllc.com

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: SQL search

2009-02-27 Thread Ga Ga

Rob Biedenharn wrote:
 On Feb 27, 2009, at 11:42 AM, Rob Biedenharn wrote:
 match_part =
 self.class.

 Rob Biedenharnhttp://agileconsultingllc.com
 r...@agileconsultingllc.com
 
 
 Sorry, if you look at the docs, sanitize_sql is a protected class
 method of ActiveRecord::Base
 

Is there some alternative way to it, and not to use protected classes?
-- 
Posted via http://www.ruby-forum.com/.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: SQL search

2009-02-27 Thread Rob Biedenharn

On Feb 27, 2009, at 2:56 PM, Ga Ga wrote:
 Rob Biedenharn wrote:
 On Feb 27, 2009, at 11:42 AM, Rob Biedenharn wrote:
 match_part =
 self.class.

 Rob Biedenharnhttp://agileconsultingllc.com
 r...@agileconsultingllc.com

 Sorry, if you look at the docs, sanitize_sql is a protected class
 method of ActiveRecord::Base

 Is there some alternative way to it, and not to use protected classes?
 --  


It's not a protected class, it's a protected method in the class  
ActiveRecord::Base which your Article class is a sub-class.  If you  
don't like the form of the call, make your own class method like this:

class Article
   def self.keyword_find(keyword)
 match_part = sanitize_sql(['MATCH(title, body) AGAINST(?)',  
keyword])
 find(:all, :select = *, #{match_part} AS score,
  :conditions = match_part, :order = 'score DESC')
   end
end

Then just call normally:

good_articles = Article.keyword_find('chocolate')

Does that make you happier?  There's nothing wrong with using  
sanitize_sql, you just need to call it from the Article class rather  
than 'directly'.

-Rob

Rob Biedenharn  http://agileconsultingllc.com
r...@agileconsultingllc.com

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---