On 27 December 2010 14:40, Phoenix Rising <polarisrising@gmail.com> wrote:
> Thank you for the reply, Colin. I should have been more descriptive,
> so let me elaborate:
>
> Say I have the following models and factories:
>
> # models
> class Game < ActiveRecord::Base
> validates :name, :presence => true, :uniqueness => true
> validates :url, :presence => true, :uniqueness => true
> end
>
> class Server < ActiveRecord::Base
> belongs_to :game
> validates :name, :presence => true
> validates :game, :presence => true
> end
>
> class Character < ActiveRecord::Base
> belongs_to :server # now we can chain character.server.game
> validates :name, :presence => true
> end
>
> # test/factories.rb
> Factory.define :game do |g|
> g.name "Foo"
> g.url "www.example.com"
> end
>
> Factory.define :server do |s|
> s.name "Bar"
> s.game { |x| x.association(:game) }
> end
>
> Factory.define :character do |c|
> c.name "Blah"
> c.server { |x| x.association(:server) }
> end
>
> Now, let's say I want to call Factory(:character). It all gets set up
> and returned properly as it should. But let's say I have the
> following in my factories thereafter:
>
> Factory.define :character2 do |c|
> c.name "Bleh"
> c.server { |x| x.association(:server) }
> end
>
> Logically, I'd expect factory_girl to try to create the :server
> object, and if it fails, look for it in the database and set the
> assignment of that association to what comes out of the DB if
> something already exists. But it doesn't do that. Instead, FG will
> cause an error in tests (not a failure, an actual error) that will say
> that the name and url are already taken. After shaking my fists in
> the air and pounding my head on the desk, I reply, "YES, I KNOW
> they're taken, that's because I want the association on an EXISTING
> OBJECT!"
>
> Am I just plain doing it wrong here? Any input you have would be
> appreciated. Thank you!
>
Yes, I think you are. Have a look at the Faker gem for generating
unique patterns.
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment