I have no idea what went wrong but I can't get belongs_to work with :class_name option. Could somebody enlighten me. Thanks a lot!
我不知道出了什么问题,但我无法使用belongs_to:class_name选项。有人可以启发我吗?非常感谢!
Here is a snip from my code.
这是我的代码中的一个剪辑。
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
environment:
环境:
ruby 1.9.1-p387 Rails 2.3.5
ruby 1.9.1-p387 Rails 2.3.5
3 个解决方案
#1
54
class User < ActiveRecord::Base
has_many :books, :foreign_key => 'author_id'
end
class Book < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id', :validate => true
end
The best thing to do is to change your migration and change author_id
to user_id
. Then you can remove the :foreign_key
option.
最好的办法是更改迁移并将author_id更改为user_id。然后你可以删除:foreign_key选项。
#2
6
It should be
它应该是
belongs_to :user, :foreign_key => 'author_id'
if your foreign key is author id. Since you actually have User class, your Book must belong_to :user.
如果您的外键是作者ID。由于您实际拥有User类,因此您的Book必须为belongs_to:user。
#3
1
I do in this way:
我是这样做的:
Migration -
移民 -
class AddAuthorToPosts < ActiveRecord::Migration
def change
add_reference :posts, :author, index: true
add_foreign_key :posts, :users, column: :author_id
end
end
class Post
班级邮报
belongs_to :author, class_name: "User"
#1
54
class User < ActiveRecord::Base
has_many :books, :foreign_key => 'author_id'
end
class Book < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id', :validate => true
end
The best thing to do is to change your migration and change author_id
to user_id
. Then you can remove the :foreign_key
option.
最好的办法是更改迁移并将author_id更改为user_id。然后你可以删除:foreign_key选项。
#2
6
It should be
它应该是
belongs_to :user, :foreign_key => 'author_id'
if your foreign key is author id. Since you actually have User class, your Book must belong_to :user.
如果您的外键是作者ID。由于您实际拥有User类,因此您的Book必须为belongs_to:user。
#3
1
I do in this way:
我是这样做的:
Migration -
移民 -
class AddAuthorToPosts < ActiveRecord::Migration
def change
add_reference :posts, :author, index: true
add_foreign_key :posts, :users, column: :author_id
end
end
class Post
班级邮报
belongs_to :author, class_name: "User"