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

  • Home
  • SEARCH
  • 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 382571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:11:44+00:00 2026-05-12T15:11:44+00:00

I have a one-to many relationship in Rails: class User < ActiveRecord::Base has_many :activities,

  • 0

I have a one-to many relationship in Rails:

class User < ActiveRecord::Base
  has_many :activities, :order => "added_at DESC"


class Activity < ActiveRecord::Base
  belongs_to :user

I have a method in Activity:

def self.test_message(user, message)
  user.activities << Activity.create do |activity|
    activity.message = message
    activity.added_at = Time.now
  end    
end

and the following unit test:

require 'test_helper'

class ActivityTest < ActiveSupport::TestCase

  def test_test_message
    #From fixture
    alice = User.find_by_name("alice")
    assert_equal 0, alice.activities.count

    Activity.test_message(alice, "Hello")
    assert_equal 1, alice.activities.count

    Activity.test_message(alice, "Goodbye")
    assert_equal 2, alice.activities.count
    assert_equal "Hello", alice.activities.find(:first).message

    #The following line fails with: Goodbye expected but was Hello
    assert_equal "Goodbye", alice.activities.find(:last).message,
    acts = alice.activities
    assert_equal 2, acts.count
    assert_equal "Goodbye", acts[1].message
  end
end

which fails on the indicated line, but I can’t work out why.

Also, using activities.find(:last) works when using the development environment, but only fails under the test environment. I have dropped and rebuilt the database.

  • 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-05-12T15:11:44+00:00Added an answer on May 12, 2026 at 3:11 pm

    This seems to be an issue with using the :order flag in your association declaration. This post isn’t the exact situation you’re experiencing, but it recommends against the practice in general:

    http://weblog.jamisbuck.org/2007/1/18/activerecord-association-scoping-pitfalls

    (I’m not sure if these suggestions are still relevant, but I was seeing the same behavior as you in Rails 2.3.3 until I made the changes below.)

    I set up your app locally and tried applying the technique from comment #4 by adding

    def Activity.by_added_at
      find :all, :order => 'added_at DESC'
    end
    

    and changing your find(:first) and find(:last) in the test to .by_added_at.first and .by_added_at.last, which returns more stable results.

    One other suggestion — your test is pretty big right now. You might consider splitting it out into multiple tests, each of which tests one or two conditions at most.

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

Sidebar

Ask A Question

Stats

  • Questions 244k
  • Answers 244k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer initWithCoder: is part of the NSCoder protocol, which is part… May 13, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer I don't think this switch does anything especially since 1.0… May 13, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer I don't think that such a style exists. But you… May 13, 2026 at 8:04 am

Related Questions

New to rails and trying to get a one to many relationship up and
I have the following entities: User Company Organization Users need to be able to
I cannot seem to get a nested form to generate in a rails view
I have a model Task which will HABTM many TaskTargets. When it comes to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.