diff --git a/app/models/user/querying.rb b/app/models/user/querying.rb index 94c195a067f70c2b16029ad0abd4a9011f89eedc..b0b1e457a2855d68e9e13b0efe152d0de26b1a6a 100644 --- a/app/models/user/querying.rb +++ b/app/models/user/querying.rb @@ -16,7 +16,7 @@ module User::Querying def visible_shareables(klass, opts={}) opts = prep_opts(klass, opts) shareable_ids = visible_shareable_ids(klass, opts) - klass.where(:id => shareable_ids).select('DISTINCT '+klass.to_s.tableize+'.*').limit(opts[:limit]).order(opts[:order_with_table]) + klass.where(:id => shareable_ids).select('DISTINCT '+klass.to_s.tableize+'.*').limit(opts[:limit]).order(opts[:order_with_table]).order(klass.table_name+".id DESC") end def visible_shareable_ids(klass, opts={}) @@ -48,7 +48,7 @@ module User::Querying def ugly_select_clause(query, opts) klass = opts[:klass] select_clause ='DISTINCT %s.id, %s.updated_at AS updated_at, %s.created_at AS created_at' % [klass.table_name, klass.table_name, klass.table_name] - query.select(select_clause).order(opts[:order_with_table]).order(klass.table_name+'.id DESC').where(klass.arel_table[opts[:order_field]].lt(opts[:max_time])) + query.select(select_clause).order(opts[:order_with_table]).where(klass.arel_table[opts[:order_field]].lt(opts[:max_time])) end def construct_shareable_from_others_query(opts) diff --git a/spec/models/user/querying_spec.rb b/spec/models/user/querying_spec.rb index 36b9b4d025ab887c8cf7330dca71052ae3b83d78..ffaeeb4a1c9c2ccba86f446cd5d08e504afdd356 100644 --- a/spec/models/user/querying_spec.rb +++ b/spec/models/user/querying_spec.rb @@ -113,6 +113,21 @@ describe User::Querying do Factory(:status_message, :public => true) bob.visible_shareables(Post).count.should == 0 end + + context 'with two posts with the same timestamp' do + before do + aspect_id = alice.aspects.where(:name => "generic").first.id + Timecop.freeze Time.now do + alice.post :status_message, :text => "first", :to => aspect_id + alice.post :status_message, :text => "second", :to => aspect_id + end + end + + it "returns them in reverse creation order" do + bob.visible_shareables(Post).first.text.should == "second" + bob.visible_shareables(Post).last.text.should == "first" + end + end context 'with many posts' do before do