I am working with FactoryGirl for Heroku Pinger. I was trying to restrict users to only create five websites.
To test this, I copied the test “creates a few websites with FactoryGirl” into a new one called “only creates five websites with FactoryGirl”. It failed. It seemed to allow the user to only create 5 sites, but at the end of the test the site count (querying on user_id) was 11. I could not figure this out.
Then it hit me: FactoryGirl was creating an array of sites using the generate_factory_sites method. I would then take that array and try to create the sites again. The user’s site count is only checked in the website controller, while FactoryGirl uses the model directly. So by the time I am checking to see how many sites a user has, I am already over the limit, and the limit is set to 0. So I changed the test to put data for websites in an array of hashes first, and use the hashes to create the sites manually.
I might go back later and clean things up. (I was about to write “I will”, but let’s be honest: how often does that REALLY happen?)
I also put in something to ensure that a user can only delete their own sites. That was a hassle.
So what I tried to do is to add some sites with one user id, and then delete them with another, and then delete them with the first user ID. It did not work. The session variable user_id will not change. I read online that you can set it like this:
session[:user_id] = JDJDDJ
but that did not work.
So I added some sites, changed the user ID of that site instance, then deleted it with the first user ID.
I created two users and changed the user_id in the website model, and then tried to delete the sites. I was not able to delete the sites after changing the user_id field in the website objects. Then I changed it back and deleted the sites.
Image from World Digital Library, assumed allowed under Fair Use. Image from the Psalter of Frederick II, a 13th century manuscript in the Byzantine style, housed at Riccardiana Library of Florence, aka Biblioteca Riccardiana.