I did post twice, but only after the first post did not appear. How strange.

I suppose I could use group by news_uid and only take one item from
each group (because they will all be the same. Does not seem like an
ideal solution though.

With DISTINCT:

  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` WHERE
(`categories`.`inews_code` = 'sport') LIMIT 1
  Category Load (0.3ms)  SELECT `categories`.* FROM `categories` WHERE
(`categories`.`inews_code` = 'top') LIMIT 1
  Category Load (0.1ms)  SELECT `categories`.* FROM `categories` WHERE
(`categories`.`path` = 'news') LIMIT 1
  News Load (17.3ms)  SELECT DISTINCT('news.*') FROM `news` INNER JOIN
`news_categories` ON `news_categories`.`news_id` = `news`.`id` WHERE
(news_categories.id != 5) AND (news_categories.id != 2) ORDER BY
sort_order desc LIMIT 100


WITHOUT:

  Category Load (0.3ms)  SELECT `categories`.* FROM `categories` WHERE
(`categories`.`inews_code` = 'sport') LIMIT 1
  Category Load (0.3ms)  SELECT `categories`.* FROM `categories` WHERE
(`categories`.`inews_code` = 'top') LIMIT 1
  Category Load (0.1ms)  SELECT `categories`.* FROM `categories` WHERE
(`categories`.`path` = 'news') LIMIT 1
  News Load (8.5ms)  SELECT `news`.* FROM `news` INNER JOIN
`news_categories` ON `news_categories`.`news_id` = `news`.`id` WHERE
(news_categories.id != 5) AND (news_categories.id != 2) ORDER BY
sort_order desc LIMIT 100




On Wed, Aug 25, 2010 at 1:05 PM, Andy Newport <[email protected]> wrote:
> Can you post the SQL that AR is generating when you run the query?
>
> On Wed, Aug 25, 2010 at 11:03 AM, Richard Hulse <[email protected]>
> wrote:
>>
>> Hi,
>>
>> I am trying to get a list of unique items under the current scenario
>> (Rails3):
>>
>> A 'News' item:
>>
>>  has_many :categories, :through => :news_categories
>>
>> I am getting a group of stories thus:
>>
>> def self.news_feed_stories
>>   joins(:news_categories).
>>   where("news_categories.id != ?", Category.sport.id).
>>   where("news_categories.id != ?", Category.top.id).
>>   order('sort_order desc')
>>  end
>>
>> But there are duplicates when a story is in two categories.
>>
>> I tried adding:
>>
>>   select("DISTINCT('news.*')").
>>
>> but I get back no stories.
>>
>>
>>
>> cheers,
>>
>> Richard
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "WellRailed" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/wellrailed?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "WellRailed" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/wellrailed?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"WellRailed" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/wellrailed?hl=en.

Reply via email to