Help me write a nicer SQL query in Rails

Posted by Sainath Mallidi on Stack Overflow See other posts from Stack Overflow or by Sainath Mallidi
Published on 2010-06-08T19:30:23Z Indexed on 2010/06/08 19:32 UTC
Read the original article Hit count: 213

Filed under:
|
|
|

Hi,

I am trying to write an SQL query to update some of the attributes that are regularly pulled from source. the output will be a text file with the following fields: author, title, date, popularity

I have two tables to update one is the author information and the other is popularity table. And the Author Active Record object has one popularity. Currently I'm doing it like this.\

arr.each {
|x|
x = x.split(" ")
results = Author.find_by_sql("SELECT authors.id 
                              FROM authors, priorities 
                              WHERE 
                              authors.id=popularity.authors_id AND 
                              authors.author = x[0]")
results[0].popularity.update_attribute("popularity", x[3])

I need two tables because the popularity keeps changing, and I need only the top 1000 popular ones, but I still need to keep the previously popular ones also. Is there any nicer way to do this, instead of one query for every new object. Thanks.

© Stack Overflow or respective owner

Related posts about sql

Related posts about ruby-on-rails