विफल रहता है मुझे नहीं पता कि क्या गलत हुआ है, लेकिन मुझे class_name विकल्प के साथ काम करने के लिए संबंधित नहीं मिल सकता है। क्या कोई मुझे प्रबुद्ध कर सकता है। आपका बहुत बहुत धन्यवाद!इसके साथ संबंधित है: class_name विकल्प
यहां मेरे कोड से एक स्निप है।
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
पर्यावरण:
रूबी 1.9.1-p387 रेल 2.3.5
मैं अच्छा विदेशी कुंजी होने के बारे में परवाह है। मैं कॉलम का नाम बदलने के बारे में सुझाव लागू नहीं करूंगा, क्योंकि कॉलम का सही नाम होने पर क्या हो रहा है यह स्पष्ट है। –
किसी भी तरह से काम करता है। जो कुछ भी आपको और आपकी टीम के लिए समझ में आता है वह सही काम है। :) –
और यह सोचने के लिए कि class_name: 'उपयोगकर्ता' पर्याप्त होगा – Donato