Skip to content

Instantly share code, notes, and snippets.

@jameslafa
Created April 16, 2015 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameslafa/a358d0b08ad2c19c55e3 to your computer and use it in GitHub Desktop.
Save jameslafa/a358d0b08ad2c19c55e3 to your computer and use it in GitHub Desktop.
FactoryGirl attribute_for does not create associations for obvious performance reason. However, sometime it is useful to get all attributes with ids of association to test a POST#create request for example. To enable, just create a macro in your spec/support folder with the following content. Then call attributes_with_foreign_keys instead of att…
module ControllerMacros
def attributes_with_foreign_keys(*args)
FactoryGirl.build(*args).attributes.delete_if do |k, v|
["id", "type", "created_at", "updated_at"].member?(k)
end
end
end
RSpec.configure do |config|
config.include ControllerMacros, :type => :controller
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment