Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8436857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:17:46+00:00 2026-06-10T07:17:46+00:00

My Rails app is getting the history of changes for two models, using the

  • 0

My Rails app is getting the history of changes for two models, using the auditor gem like so:

@audit = Audit.where( :auditable_id => current_user.posts,
                      :auditable_type => "Post") +
         Audit.where( :auditable_id  => @comments,
                      :auditable_type => "Comment")

This works, but then I need to sort the whole @audit variable by the time the change was made.

I have two issues to solve.

  1. the following methods have not worked: sort, sort_by, order
  2. I need to figure out which of the following fields I need to sort by:

    => Audit(id: integer, auditable_id: integer, auditable_type: string, owner_id: integer, owner_type: string, user_id: integer, user_type: string, action: string, audited_changes: text, version: integer, comment: text, **created_at**: datetime) 
    
    1.9.3-p194 :002 > Audit.last
    Audit Load (168.0ms)  SELECT "audits".* FROM "audits" ORDER BY version DESC, created_at DESC LIMIT 1
    => #<Audit id: 5, auditable_id: 58, auditable_type: "Post", owner_id: 58, owner_type: "Post", user_id: 1, user_type: "User", action: "update", audited_changes: {"status"=>["to approve", "to review"], " **updated_at** "=>[2012-08-24 15:29:26 UTC, 2012-08-24 19:29:52 UTC]}, version: 2, comment: "post modified by Bruno Amaral ", created_at : "2012-08-24 19:29:52"> 
    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:17:47+00:00Added an answer on June 10, 2026 at 7:17 am

    You should be able to build a single query to load all of the Audit objects you’re interested in. Since it’s a single query, the database can handle the sorting too.

    The SQL you want to execute looks something like this:

    SELECT *
    FROM audits
    WHERE
      auditable_type = 'Post' AND auditable_id = … OR
      auditable_type = 'Comment' AND auditable_id IN (…)
    ORDER BY created_at
    

    Which you should be able to build using Arel with something like this (assuming you’re using Rails 3):

    t = Audit.arel_table
    for_post = t[:auditable_type].eq('Post').and(t[:auditable_id].eq(post.id))
    for_comments = t[:auditable_type].eq('Comment').and(t[:auditable_id].in(comment_ids))
    
    audits = Audit.where(for_post.or(for_comments)).order(:created_at)
    

    For more information about building complex queries with Arel, see ASCIIcasts episode 215 and the Arel README.

    If you don’t like the Arel syntax, you can also use find_by_sql or pass a string to where, but bear in mind that using Arel will shield you from various kinds of errors and subtle difference between databases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple rails app using Postgres DB, getting 'integer out of range' error when trying
I have a rails app that is parsing HTML using the nokogiri gem version
I just deployed a rails app using passenger. Now i am getting a 404
We are getting close to releasing our new rails app and so far interest
I'm getting the following error in my rails app: comparison of User with User
I have a rails app that has asynchronous processing, and I'm having trouble getting
I am getting a bug when I use a text_area in my rails app.
I'm trying to upload a rails app to dotcloud. I'm getting this error: PG::Error
I'm having some trouble getting Stripe up and running in my Rails app. When
I'm trying to create a rails app, but somethings going wrong, I'm getting a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.