views:

100

answers:

1

How do I translate the following into a named_scope?

  def self.commentors(cutoff=0)
    return User.find_by_sql("select users.*, count(*) as total_comments from users, comments 
      where (users.id = comments.user_id) and (comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null) 
      group by users.id having total_comments > #{cutoff} order by total_comments desc")
  end

Here's what I have right now but it doesn't seem to work:

  named_scope :commentors, lambda { |count=0|
    { :select => "users.*, count(*) as total_comments",
      :joins => :comments,
      :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
      :group => "users.id",
      :having => "total_comments > #{count.to_i}",
      :order => "total_comments desc"
    }
  }

Ulimately, this is what worked;

  named_scope :commentors, lambda { |*args|
      { :select => 'users.*, count(*) as total_comments',
        :joins => :comments,
        :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil} },
        :group => 'users.id',
        :having => ['total_comments > ?', args.first || 0],
        :order => 'total_comments desc' }
      }  
A: 

The problem you have is because you replaced selecting from users, comments with an inner join with comments. To properly translate the SQL to a named_scope, use this:

named_scope :commentors, lambda { |cutoff|
    { :select => 'users.*, count(*) as total_comments',
      :conditions => { :comments => { :public_comment => 1, :aasm_state => 'posted', :talkboard_user_id => nil } },
      :from => 'users, comments',
      :group => 'users.id',
      :having => ['total_comments > ?', cutoff],
      :order => 'total_comments desc' } }

And you can't have a default value for lambda parameters, so you can't use |cutoff = 0|.

Samuel
Not quite getting the result I'd expect. What's the right syntax for :talkboard_user_id => nil? The field must be nil as a condition, but I wonder if this is the right syntax?
keruilin
To set a defult value, could I do this? :having => ['total_comments > ?', cutoff.nil? ? 0 : cutoff]
keruilin
{:field => nil} is the correct syntax, you'll have to post the complete SQL query to see why it's failing that condition.
Samuel
Prob isn't issue, as I tried this condition statement: conditions => "(comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null)"
keruilin
You can use cutoff.nil ? 0 : cutoff to have a default value, but you'll have to live with warnings every time you use it without a value.
Samuel
Here's how it's translated: SELECT users.*, count(*) as total_comments FROM users, comments WHERE ((comments.public_comment = 1) and (comments.aasm_state = 'posted') and (comments.talkboard_user_id is null)) GROUP BY users.id HAVING total_comments > 25 ORDER BY total_comments descWhen I use find_by_sql, I get 9 results, but with above named_scope I get like 18k.
keruilin
Try adding users.id = comments.user_id as well to the conditions hash.
Samuel
To make cutoff parameter optional, you can use ` lambda { |*args| ...`, and then `:having => ['total_comments > ?', args.first || 0]`
Voyta
There reason I was getting different results was because I was using count on the named_scope, which just counted comments. I should have use length.
keruilin