No i trafiłem na kolejny problem ale tu nie wiem jak się za to zabrać. Tabela books jest w relacji many-to-many z tabelą authors. Chcę ustawić validację tak by zawsze dana książka miała co najmniej jednego autora.
Migracja na podstawie pliku:
[code]class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books, :options => ‘DEFAULT CHARSET=utf8’ do |t|
t.string :title, :limit => 255, :null => false
t.integer :publisher_id, :null => false
t.datetime :published_at
t.string :isbn, :limit => 13, :unique => true
t.text :blurb
t.integer :page_count
t.float :price
t.timestamps
end
create_table :authors_books, :id => false do |t|
t.integer :author_id, :null => false
t.integer :book_id, :null => false
end
end
def self.down
drop_table :books
drop_table :authors_books
end
end[/code]
Model wygląda następująco:
[code]class Book < ActiveRecord::Base
belongs_to :publisher
has_and_belongs_to_many :authors
validates_length_of :title, :in => 2…255
validates_presence_of :publisher
validates_presence_of :published_at
validates_numericality_of :page_count, :only_integer => true
validates_numericality_of :price
validates_uniqueness_of :isbn
validates_format_of :isbn, :with => /[0-9-xX]{13}/
validates_presence_of :authors
end[/code]
Test przeprowadzam na podstawie pliku:
[code]require ‘test_helper’
class BookTest < ActiveSupport::TestCase
fixtures :authors, :publishers
def test_create
book = Book.new(
:title => ‘Ruby for Toddlers’,
:publisher_id => publishers(:one).id,
:authors => [authors(:one)],
:isbn => ‘123-123-123-1’,
:blurb => ‘The best book since “Bodo Bar”’,
:page_count => 12,
:price => 40.4
)
assert book.save
end
end[/code]
Wynik testu:
[code]test_create(BookTest)
[./test/unit/book_test.rb:18:in test_create' /usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in
send’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run’]:
is not true.
1 tests, 1 assertions, 1 failures, 0 errors[/code]
Zastanawia mnie wpis w test.log:
Unable to load authors_book, underlying cause no such file to load -- authors_book
Tylko dlaczego authors_book a nie authors_books?