Hey Daryl,
I've dealt with a similar issue before and solved it using something like
this (disclaimer, I haven't actually tested the following code):
factory :pd_request do
association :employee, :factory => :employee_with_supervisor
description { Faker::Lorem.words.join(' ') }
start_date 7.days.from_now
trait :approved do
status 'Approved'
name 'Lorem Ipsum...'
association :approving_manager, :factory => :employee # I'm assuming
this can be a new employee
after(:build) do |pd_request|
pd_request.approving_supervisor = pd_request.employee.supervisor
end
end
end
You'd create an approved pd_request with FactoryGirl.create(:pd_request,
:approved)
I'm a fan of using traits instead of nested Factories and have found after
:create and after :build callbacks really useful for setting up
associations that need to know about existing objects.
I hope the code above works, or at least helps you get closer to a solution.
Cheers,
Shevaun
On Friday, 9 November 2012 18:41:26 UTC+13, Daryl wrote:
>
> I've been trying to get a factory to refer back to an AR relation set by
> another created factory. I've asked around and come up short so thought I'd
> throw it out to the list.
>
> Trying to test a bit of accounting business logic and setting an employee
> with a supervisor (which is a self-referential relation employees are
> supervised by another employee) who then makes a request which needs to get
> approved by that employee's supervisor (like leave, pd, etc). It may just
> be a syntax thing, but keep getting AR:AssociationTypeMismatch errors... to
> wit:
>
> factory :pd_request do
> association :employee, :factory => :employee_with_supervisor
> description { Faker::Lorem.words.join(' ') }
> start_date 7.days.from_now
>
> factory :pd_request_approved do |pd_request|
> *association :approving_supervisor, :factory => :employee *
> association :approving_manager, :factory => :employee
> name "Lorem ipsum..."
> status "Approved"
> end
> end
>
> Basically, the current situation is using a *random new* supervisor for
> this test, but trying to fix this properly and test the functionality
> properly, the approved request should be set to the
> employee_with_supervisor's supervisor. So, in the factory
> pd_request_approved, whatever the syntactical equivalent of
> pd_request.employee.supervisor would be to set that association.
>
> I've run this by two way-better-at-FG-than-I people without luck and after
> trying some "this ought to work" thrashing, so think I'm eiher getting into
> over-my-head syntax territory or it's out of bounds for FG.
>
> Anyone seen or had a similar issue before?
>
> thanks!
> Daryl.
> PS> In case you need to know how the employee with supervisor factory is
> set...
>
> factory :employee do
> first_name { Faker::Name.first_name }
> last_name { Faker::Name.last_name }
> email { Faker::Internet.email }
> ...
>
> factory :employee_with_supervisor do
> association :supervisor, :factory => :employee
> end
> end
>
>
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rails-oceania/-/5SSe7gbxuzIJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rails-oceania?hl=en.