Help me write a nicer SQL query in Rails
- by Sainath Mallidi
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.