Skip to content

Instantly share code, notes, and snippets.

@distributedlife
Created April 11, 2012 12:51
Show Gist options
  • Save distributedlife/2359122 to your computer and use it in GitHub Desktop.
Save distributedlife/2359122 to your computer and use it in GitHub Desktop.
Is this necessary?
def with object
yield(object)
end
#so I can do this:
with Project.create do |project|
Task.create(:project_id => project.id)
end
@distributedlife
Copy link
Author

yes; the answer is Object#tap

Project.create.tap do |project|
...
end

@rhunter
Copy link

rhunter commented Apr 11, 2012

Your with version reads nicely, and equivalent to:

Project.create.tap do |project|
  Task.create(:project_id => project.id)
end

If Task and Project are ActiveRecord models, you could use:

Task.create(:project => Project.create)

And if it's an integration test setup, you might consider FactoryGirl or Machinist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment