Rails fixtures - defining a table name?
Asked Answered
S

3

9

At the minute all my fixtures have the same name as the table that they are intended for, because of a recent issue with rails it doesn't seem possible to have a fixture beginning with the word 'test'

Does anyone know of a way to have a different fixture name and then map it to the correct table?

Thanks, Andy

Subhead answered 21/12, 2009 at 11:27 Comment(0)
P
0

This blog post looks similar to what you want to do.

Palmette answered 29/12, 2009 at 1:16 Comment(1)
This blog post link is deadAnother
R
4

You can set the class of a given fixture manually like so:

class SomeTest < ActiveSupport::TestCase

  set_fixture_class widgets: 'Module::ClassInAModule'
  fixtures :widgets # or fixtures :all if you’ve defined all the mappings required

  test 'widgets can be found' do
    assert Module::ClassInAModule.all.any?, 'there should be widgets'
  end

end

Depending on how your tests/test helpers are set up, you may want to move this call to a parent class or something.

Regalado answered 3/3, 2014 at 13:28 Comment(0)
M
2

In your model set this keyword:

class Anywhere < ApplicationRecord
    self.table_name = "singular_table"
end

OBS: (Rails >= 5)

Marketable answered 6/2, 2019 at 13:21 Comment(1)
It just takes care of everything.Extern
P
0

This blog post looks similar to what you want to do.

Palmette answered 29/12, 2009 at 1:16 Comment(1)
This blog post link is deadAnother

© 2022 - 2024 — McMap. All rights reserved.