Forum: Ruby on Rails Locating an arbitrary fixture...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric D. Nielsen (Guest)
on 2006-06-13 22:26
Is there a good way to get an arbitrary fixture from the generated
fixture function?

I need a way to get one of my fixtures, I don't care which one.  It
doesn't need to be random and could always return the same fixture.
This is part of a support function for custom assertions and it doesn't
know what class its working on, let alone what fixture names are
possible.

I've tried digging around some in the foxture source file to see exactly
how they are implemented, but its not quite clear yet.  I've been trying
to do something (very messy) like:
  tmp =
@@already_loaded_fixtures[self.class][@klass.to_s.downcase.pluralize]

  tmp is now a double nested array.  However it appears to be protected
in some manner as tmp[0] returns nil.  If I flatten the list, the
"protection' is still present.  I can't access the elements by index.  I
can't get at them via tmp.values, etc.  I can iterate via each: tmp.each
{|x| puts x.inspect } displays the contents.  So it seems like I'm
definitly trying to get at something in a manner that I shouldn't be.

All I want is to be able to basically do a
  x = users(:some_user) without knowing either "users" or
":some_user"... the first part is easy:
self.send(@klass.to_s.downcase.pluralize, ....?).  If I had a good way
to get a list of names within the fixture that would complete the
picture.  Or is there some alternate way that I'm missing...

Any suggestions?

Eric
Alex Wayne (Guest)
on 2006-06-14 01:11
Eric D. Nielsen wrote:
> All I want is to be able to basically do a
>   x = users(:some_user) without knowing either "users" or
> ":some_user"... the first part is easy:
> self.send(@klass.to_s.downcase.pluralize, ....?).  If I had a good way
> to get a list of names within the fixture that would complete the
> picture.  Or is there some alternate way that I'm missing...
>
> Any suggestions?
>
> Eric

You can use ActiveRecord finders in tests.

  def foo(klass)
    klass.find(1)
  end

  foo(User) #=> user object with id=1
  foo(Post) #=> post object with id=1

That might work better that using named fixtures if you don;t care about
the names.
Eric D. Nielsen (Guest)
on 2006-06-14 04:02
Alex Wayne wrote:
> Eric D. Nielsen wrote:
>> All I want is to be able to basically do a
>>   x = users(:some_user) without knowing either "users" or
>> ":some_user"... the first part is easy:
>> self.send(@klass.to_s.downcase.pluralize, ....?).  If I had a good way
>> to get a list of names within the fixture that would complete the
>> picture.  Or is there some alternate way that I'm missing...
>>
>> Any suggestions?
>>
>> Eric
>
> You can use ActiveRecord finders in tests.
>
>   def foo(klass)
>     klass.find(1)
>   end
>
>   foo(User) #=> user object with id=1
>   foo(Post) #=> post object with id=1
>
> That might work better that using named fixtures if you don;t care about
> the names.

self.smack...  such a simple answer to what I made into a complicated
mess.  Thank you.

Eric
This topic is locked and can not be replied to.