#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Accessing element in an array


    hello...
    i'm new to ruby so apologies in advance.
    I have the following code

    ids = Testcase.find(:all, :select=>"id")
    for num in (0..ids.length)
    run1 = Testrun.create(:build=>"2.0.0", :testcase_id=>ids[num])
    end

    The values for ids array are 22,23,24
    When I try this

    Testrun.create(:build=>"4.0.0.0", :testcase_id=>ids[0]) I get the following in the console
    #<Testrun id: 50, testcase_id: 1, created_at: "2008-12-04 00:06:01", updated_at: "2008-12-04 00:06:01">

    I can't figure out why testcase_id is 1, it should be 22 since ids[0]=22

    Can someone please help.

    Thanks

    Ali
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    Just to clarify, you run the first segment of code immediately before the second one in the console?

    Depending on which version of Rails you are running, you might be able to do it this way:
    Ruby Code:
    Testrun.create(:build => "4.0.0.0", :testcase => Testcase.first)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0
    Hi Destin
    So to clarify, the code above is from a migration I was trying to create. I have a model (Testcase) which has a few rows. I have another model (Testrun) whose Foreign key is testcase_id.

    In my migration i am trying to add some rows for Testrun

    ids = Testcase.find(:all, :select=>"id")
    for num in (0..ids.length)
    run1 = Testrun.create(:build=>"2.0.0", :testcase_id=>ids[num])
    end

    This seems to be working but testcase_id=>ids[num] does not get the correct value.

    I then tried playing directly with active record using rails console.

    If I try
    Testrun.create(:build=>"4.0.0.0", :testcase_id=>22)
    it works

    but
    Testrun.create(:build=>"4.0.0.0", :testcase_id=>ids[0])
    gives me
    #<Testrun id: 50, testcase_id: 1, created_at: "2008-12-04 00:06:01", updated_at: "2008-12-04 00:06:01">
    testcase_id = 1 is not what I expect.

    hopefully this clears up things.
    I was reading somewhere at the ids[0] actually is a hash and not a integer. a little confused
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    Can you post the entire migration file? Also, try typing ids[0] on a new line in the console to see what its value is.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0
    class AddTestData < ActiveRecord::Migration
    def self.up
    down #delete all test data
    test1 = Testcase.create(:title=>"testcase1", :desc=>"this is lame", roject=>"project1", :testsuite=>"testsuite1")
    run1 = Testrun.create(:build=>"1.0.0", :status=>"passed")
    test1.testruns << run1

    test1 = Testcase.create(:title=>"testcase2", :desc=>"this is lame", roject=>"project1", :testsuite=>"testsuite1")
    run1 = Testrun.create(:build=>"1.0.0", :status=>"passed")
    test1.testruns << run1


    #build 2.0.0 data
    ids = Testcase.find(:all, :select=>"id")
    for num in (0..ids.length)
    run1 = Testrun.create(:build=>"2.0.0", :status=>"passed", :testcase_id=>ids[num])
    end
    end

    def self.down
    Testrun.delete_all
    Testcase.delete_all
    end
    end

    This is the output of the console
    >> ids = Testcase.find(:all, :select=>"id")
    => [#<Testcase id: 26>, #<Testcase id: 27>]

    >> ids[0]
    => #<Testcase id: 26>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    Oh, so "ids" is an array of Testcase objects. Try one of the following:
    Ruby Code:
    Testrun.create(:build => "4.0.0.0", :testcase => ids[0])
    Testrun.create(:build => "4.0.0.0", :testcase_id => ids[0].id)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0
    Thanks a lot destin. This has answered my question.

IMN logo majestic logo threadwatch logo seochat tools logo