tispratik / bs

BS
1 stars 0 forks source link

Ritvij - Correct Query #7

Closed TheHumaneClub closed 14 years ago

TheHumaneClub commented 14 years ago

This below query works in MYSQL but in Ruby it gives only Alertable_id and Alertable_type in output. Alert.all(:select => "alertable_id, alertable_type, max(id)", :conditions => {:project_id => projectid }, :group => "alertable_id, alertable_type", :limit => count)

Bottom line, presently alerts is working query Alert.all(:conditions => {:project_id => projectid }, :order => "id desc", :limit => count)

Possibly give it to Sergey to correct Query

svarks commented 14 years ago
>> Alert.all(:select => "alertable_id, alertable_type, max(id) as max_id", :conditions => {:project_id => 1 }, :group => "alertable_id, alertable_type", :limit => 10).first.max_id
Alert Load (5.6ms)  SELECT alertable_id, alertable_type, max(id) as max_id FROM `alerts` WHERE (`alerts`.`project_id` = 1) GROUP BY alertable_id, alertable_type ORDER BY id desc LIMIT 10
=> "97"
>>

Is it what you wanted?

TheHumaneClub commented 14 years ago

The idea is to print only the TOP MOST comment of each alertable_type, alertable_id

svarks commented 14 years ago

So I think it should be calculated separately for tasks, wikis, articles, etc.

TheHumaneClub commented 14 years ago

Dont Agree. Alertable_type and alertable_id is the only UNIQUE way to identity INDIVIDUAL task, wiki, etc

TheHumaneClub commented 14 years ago

Alertable_type => Article / Asset / Task / WikiPage

tispratik commented 14 years ago

What say??

svarks commented 14 years ago

you can't do it in one query anyway

TheHumaneClub commented 14 years ago

We are using 2 queries already. Check Alerts model. There is self.forproject which is supposed to return alertable_type, alertable_id, max_id Finally, in display we query again Alert.find_by_id(max_id) to get all details of Alerts

TheHumaneClub commented 14 years ago

clo