Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@keating
Last activeAugust 29, 2015 13:59
    • Star(0)You must be signed in to star a gist
    • Fork(0)You must be signed in to fork a gist
    Save keating/10804240 to your computer and use it in GitHub Desktop.
    Keating's Test
    #1. The first Test
    #The command is,
    #rails g migration AddPermalinkToPosts permalink:string:uniq
    #In the migration file that just generated, I will add some code, then the migration file should be,
    classAddPermalinkToPosts <ActiveRecord::Migration
    defchange
    add_column:posts,:permalink,:string
    add_index:posts,:permalink,unique:true
    Post.select("id,title").eachdo |post|
    Post.find(post.id).update_attribute:permalink,"#{post.id}-#{post.title.split(' ').join('-')}"
    end
    end
    end
    #2. The second Test
    deffirst_methodnumber
    # change the number to a arrary, and reverse the elements' order
    arr=number.to_s.split("").reverse
    # change value of the elements that have a even index
    0.upto(arr.length -1).eachdo |i|
    ifi %2 ==0
    two_digit_array=(arr[i].to_i *2).to_s.split("")
    arr[i]=two_digit_array[0].to_i +two_digit_array[1].to_i
    end
    end
    # sum of the elements, modal with 10, and compare the result with 0
    arr.inject(0){|sum,x|sum +x.to_i} %10 ==0
    end
    putsfirst_method(7992739871)
    # this method is very like the first one
    defsecond_methodnumber
    arr=number.to_s.split("").reverse
    0.upto(arr.length -1).eachdo |i|
    ifi %2 ==0
    two_digit_array=(arr[i].to_i *2).to_s.split("")
    arr[i]=two_digit_array[0].to_i +two_digit_array[1].to_i
    end
    end
    num=arr.inject(0){|sum,x|sum +x.to_i} %10
    number.to_s +num.to_s
    end
    putssecond_method(7992739871)
    #3. The Third Test
    #First, add a unique constraint for service_id and line_item_id
    classAddUQServiceLineItemToPosts <ActiveRecord::Migration
    defup
    ActiveRecord::Base.connection.execute("ALTER TABLE payments ADD CONSTRAINT uq_service_line_item UNIQUE (service_id, line_item_id)")
    end
    defdown
    ActiveRecord::Base.connection.execute("ALTER TABLE payments DROP index uq_service_line_item")
    end
    end
    #Then, add a with method for Payment, the most important thing here is adding a lock
    classPayment <ActiveRecord::Base
    belongs_to:service
    defself.withhash
    Payment.transactiondo
    payment=Payment.where(hash).first_or_create
    payment.lock!
    yield(payment)
    end
    end
    end
    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp