Neyin yanlış gittiğine dair hiçbir fikrim yok ama belongs_a ile çalışamıyorum: class_name option. Birisi beni aydınlatır. Çok teşekkürler!belongs_to ile: class_name seçeneği başarısız oldu
İşte kodumdan bir parça.
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.text :name
end
end
def self.down
drop_table :users
end
end
#####################################################
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.text :title
t.integer :author_id, :null => false
end
end
def self.down
drop_table :books
end
end
#####################################################
class User < ActiveRecord::Base
has_many: books
end
#####################################################
class Book < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :validate => true
end
#####################################################
class BooksController < ApplicationController
def create
user = User.new({:name => 'John Woo'})
user.save
@failed_book = Book.new({:title => 'Failed!', :author => @user})
@failed_book.save # missing author_id
@success_book = Book.new({:title => 'Nice day', :author_id => @user.id})
@success_book.save # no error!
end
end
ortamı: yabancı anahtar yazar kimliği ise
yakut 1.9.1-P387 Raylar 2.3.5
. Sütunun doğru ismine sahip olduğunda neler olup bittiği daha net olduğu için, sütunu yeniden adlandırmayla ilgili öneriyi uygulamam. –
Her iki durumda da çalışıyor. Siz ve ekibiniz için mantıklı olan şey, yapılacak en doğru şey. :) –
Ve class_name düşünmek için: 'Kullanıcı' yeterli olurdu – Donato