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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:12:13+00:00 2026-06-18T01:12:13+00:00

I am testing a method on my group model. It relies on creating a

  • 0

I am testing a method on my group model. It relies on creating a few badgetypes through a factory.

Here is the code

it "add itself to badges with method add_to_badgetypes" do
    badge = FactoryGirl.create(:badgetype)
    old_badge_ids = badge.group_ids.split(",")
    badge2 = FactoryGirl.create(:badgetype, :group_ids => "3,4,5")
    group.add_to_badgetypes(badge.id, badge2.id)
    expect( badge.group_ids ).to eq( old_badge_ids.push(group.id).join(",") ) 
end

‘

badge.group_ids’ is “200, 300” before, and should be “200,300,#{group.id}” after. When I’m tailing the log, I see this:

  `   Badgetype Load (0.5ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 40 LIMIT 1

   (0.2ms)  SAVEPOINT active_record_1

   (0.3ms)  UPDATE `badge_types` SET `group_ids` = '200,300,15' WHERE `badge_types`.`id` IS NULL

   (0.2ms)  RELEASE SAVEPOINT active_record_1

  Badgetype Load (0.4ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 41 LIMIT 1

   (0.1ms)  SAVEPOINT active_record_1

   (0.3ms)  UPDATE `badge_types` SET `group_ids` = '3,4,5,15' WHERE `badge_types`.`id` IS NULL

   (0.1ms)  RELEASE SAVEPOINT active_record_1

   (0.3ms)  ROLLBACK

   (0.1ms)  BEGIN

`

So it is successfully updating the group_ids, but for some reason the test fails, and if I pp badge.group_ids I get the original value (200,300), and not the updated value.

I assume this has something to do with ‘SAVEAPOINT active_record_1’, then the UPDATE, and then RELEASE SAVEPOINT. If so, why?

How can I get this spec to pass, as it seems to all be messing up in the db, not the method.

Thanks

EDIT

Found out some more interesting information by playing around in the console. If I create a factory and assign it it one line:

g = FactoryGirl.create(:group)

and then run:

g.add_to_badgetype(1,2)

I get this SQL

1.9.3-p286 :080 > g.add_to_badgetypes(1, 2)
  Badgetype Load (0.5ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.2ms)  UPDATE `badge_types` SET `group_ids` = 'test,8,9,10' WHERE `badge_types`.`id` IS NULL
   (0.1ms)  COMMIT
  Badgetype Load (0.3ms)  SELECT group_ids FROM `badge_types` WHERE `badge_types`.`id` = 2 LIMIT 1
   (0.1ms)  BEGIN
   (0.2ms)  UPDATE `badge_types` SET `group_ids` = '200,300,8,9,10' WHERE `badge_types`.`id` IS NULL
   (0.1ms)  COMMIT
 => [1, 2] 

Notice the odd UPDATE statement “WHERE badge_types.id IS NULL”

If after I create the factory, I reassign it to a variable and then run the same command, it works find:

1.9.3-p286 :081 > g = Group.last
  Group Load (0.5ms)  SELECT `groups`.* FROM `groups` ORDER BY `groups`.`id` DESC LIMIT 1
 => #<Group id: 10, group_name: "Test Group", short_name: "test_group", enrollment: false, kiosk: false, landing: 0, ro_landing: 0> 
1.9.3-p286 :082 > g.add_to_badgetypes(1, 2)
  Badgetype Load (0.6ms)  SELECT `badge_types`.* FROM `badge_types` WHERE `badge_types`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE `badge_types` SET `group_ids` = 'test,8,9,10' WHERE `badge_types`.`id` = 1
   (0.2ms)  COMMIT
  Badgetype Load (0.4ms)  SELECT `badge_types`.* FROM `badge_types` WHERE `badge_types`.`id` = 2 LIMIT 1
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE `badge_types` SET `group_ids` = '200,300,8,9,10' WHERE `badge_types`.`id` = 2
   (0.2ms)  COMMIT
 => [1, 2] 

So apparently groups being a factory is causing the SQL update statement to look for ids of NULL.

Any ideas?

  • 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-18T01:12:14+00:00Added an answer on June 18, 2026 at 1:12 am

    Aaaaaaand I figured it out.

    I just needed to reload the variable “badgetype”

    before the test for equality, I added

    @badge.reload

    et voila. Weird.

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

Sidebar

Related Questions

I'm having trouble unit testing a method that changes some properties of a reference
When I'm testing a void method there is nothing to assert.For example a CreateSomething
Edit #2: Does anyone have a good method of testing the middle of a
Should one create unit tests involving IO? Ie, testing a class method for serializing/deserializing
I am testing a web application using Java Remote Method Invocation (RMI). When I
In unit testing, the setup method is used to create the objects needed for
I've got a method in my class only for testing purpose : private void
I've testing some new CLR 4.0 behavior in method inlining (cross-assembly inlining) and found
I'm testing a page with Selenium IDE and want to execute a method already
I'm wondering how to go about testing this. I have a method that takes

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.