Created
January 26, 2012 03:23
-
-
Save kinopyo/1680766 to your computer and use it in GitHub Desktop.
FactoryGirl: How to define a value based on an already defined one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Say you have two columns: start_at, end_at | |
# When start_at is defined, you want end_at's value based on the start_at | |
# Here is the solution, passing a block to get the object of the context. | |
start_at Random.new.rand(14..45).days.from_now | |
end_at { |l| l.start_at + 2.hours } | |
# Full example | |
FactoryGirl.define do | |
factory :lesson do | |
sequence(:title) { |n| "lesson title#{n}" } | |
sequence(:description) { |n| "lesson description#{n}" } | |
capacity 6 | |
start_at Random.new.rand(14..45).days.from_now | |
end_at { |l| l.start_at + 2.hours } | |
price [5000, 5100, 5200, 5300, 5500, 6000].sample | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment