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 6604605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:10:46+00:00 2026-05-25T19:10:46+00:00

I have 2 models. A User and a Task . Here’s the code for

  • 0

I have 2 models. A User and a Task. Here’s the code for them both:

class User < ActiveRecord::Base
  has_many :tasks
  has_many :assigned_tasks, :class_name => 'Task', :foreign_key => 'assigned_user_id'
end

class Task < ActiveRecord::Base
  belongs_to :user
  belongs_to :assigned_user, :class_name => 'User', :foreign_key => 'assigned_user_id'
end

The schema is quite obvious, but for consistency, this is how it looks:

ActiveRecord::Schema.define(:version => 20110925050945) do
  create_table "tasks", :force => true do |t|
    t.string   "name"
    t.integer  "user_id"
    t.integer  "assigned_user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end

I’ve added a test case for the assigned_tasks relationship. It looks like this:

class UserTest < ActiveSupport::TestCase
  test "assigned tasks" do
    u1 = User.create(:name => 'john')
    u2 = User.create(:name => 'dave')

    assert_empty u2.assigned_tasks # LOOK AT ME

    task = u1.tasks.create(:name => 'some task', :assigned_user_id => u2.id)

    assert_equal 1, u2.assigned_tasks.size
  end
end

Now, this test case fails. It fails on the last assertion. If I remove the previous assertion (marked ‘LOOK AT ME’), this test passes fine. It also passes fine if I change this line to assert u2.assigned_tasks. Meaning it appears to break when, and only when, empty? is called against u2.assigned_tasks. Where that assertion passes, the following one fails. Here’s the failure:

UserTest:
     FAIL assigned tasks (0.12s) 
          <1> expected but was
<0>.
          test/unit/user_test.rb:12:in `block in <class:UserTest>'

So, it appears once empty? is called on the original u2.assigned_tasks Array, the task is not actually added/associated with it’s assigned user. This however appears to work fine in console.

Apologies if I’m completely overlooking something simple here, but I really can’t make any sense of this. Any points in the right direction would be extremely helpful. Thanks

PS: Rails 3.1 with a vanilla application

  • 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-25T19:10:46+00:00Added an answer on May 25, 2026 at 7:10 pm

    You need to reload the assigned_tasks, or u2.

    # This line causes assigned_tasks to be loaded and cached on u2.  Not the calling
    # of empty?, but rather the loading of the association.
    assert_empty u2.assigned_tasks
    
    # but then you actually make the task here
    task = u1.tasks.create(:name => 'some task', :assigned_user_id => u2.id)
    
    # so when this assertion happens, u2 already has an empty set of tasks cached, 
    # and fails
    assert_equal 1, u2.assigned_tasks.size
    
    # however either of these should pass
    assert_equal 1, u2.assigned_tasks(true).size
    assert_equal 1, u2.reload.assigned_tasks.size
    

    The inverse_of option serves to improve in-memory association behavior, and might also solve your problem (without reloading). Read about that here. It would look something like this (but again I’m not positive it will work in this case):

    # on User
    has_many :assigned_tasks, ..., :inverse_of => :assigned_user
    
    # on Task
    belongs_to :assigned_user, ..., :inverse_of => :assigned_tasks
    
    # in your test you might have to change the task creation to:
    u1.tasks.create(:name => 'some task', :assigned_user => u2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have models: class User < ActiveRecord::Base has_many :user_skills end class UserSkill < ActiveRecord::Base
I have a 3 models class Task < ActiveRecord::Base belongs_to :user, :dependent => :destroy
I have models as follows: User has_many goals, Goal has_many tasks, Task has_many day_tasks.
So I have a User model that :has_many other models like Documents, Videos, Posts
I have two models indexed for searching (User and Item). I'm trying to do
Suppose you have two models, User and City, joined by a third model CityPermission:
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass
In my Rails application, I have two models, Articles and Projects, which are both
I'm learning Rails by writing simple TODO tasks aplication. Two models are: class List

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.