I have the following models:
class Company < ActiveRecord::Base
has_and_belongs_to_many :regions
class Region < ActiveRecord::Base
has_many :requests
has_and_belongs_to_many :companies
class RequestForProposals < ActiveRecord::Base
belongs_to :region
Whenever I get a new request, I want to send a notification to the companies active in the same region.
How do I set this up in my fixtures so that I can unit test the logic of finding the right companies?
I've tried
region_ids: 1, 2
regions: one, two
in companies.yml, but neither works in assigning regions to the companies.
Here is a gist of the SQL generated: https://gist.github.com/2713518
For
regions: one, two
in companies.yml
to work you need to let rails auto assign the ids for the regions. This is because (in order to avoid having to have read regions.yml before companies.yml) rails calculates the ids it sticks in the join table from the names of the companies fixtures. if you've assigned the ids yourself, they won't match up with the calculated ones.
From the sql you've provided it looks like you're setting the ids on the regions to 1 and 2, i.e. your regions.yml has
one:
id: 1
name: MyString
Remove the id:1
and you should be ok. You'll also need to update any other files (e.g. request_for_proposals.yml) that refer to regions, replacing
region_id: 1
with
region: one
Rails will know that means to set region_id
to the id for the region with label one
in your fixtures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With