Rails 3、创建主记录后如何添加关联记录(Books、Auto Add BookCharacter) [英] Rails 3, how add a associated record after creating a primary record (Books, Auto Add BookCharacter)
问题描述
Rails 新手...试图了解做事的正确方式...
Rails newbie... trying to understand the right way to do things...
在我的应用程序中,用户可以创建一本书(我有这本书)
In my app users can create a Book ( I have that working)
我想要发生的是当用户创建一本书时,将一条记录添加到 BookCharacters 表中,例如 (id, book.id, user.id, characterdescription.string.)
What I want to happen is when a user creates a book, a record is added to the BookCharacters Table, something like (id, book.id, user.id, characterdescription.string.)
创建图书时,应自动将创建它的用户添加为第一个 BookCharacter.之后,用户可以根据需要手动添加/编辑任意数量的 BookCharacters.但最初我希望它们默认自动添加.
When the book is created, the user who created it should automatically be added as the first BookCharacter. After than the user can then manually add/edit as many BookCharacters as they want. But initially I want them added automatically by default.
所以在我的 Book 控制器中,我有:
So in my Book controller I have:
def create
@book = Book.new(params[:book])
respond_to do |format|
if @book.save
....
有了Rails,是不是在书保存后添加那种逻辑的做法?类似的东西
With Rails, is it the practice to add that kind of logic after the book is saved? Something like
Book.create( :creator => current_user.id)
感谢您的帮助
推荐答案
需要理解的重要一点是 Rails 使用 ActiveRecord 实现关系的约定.一本书有很多字符,每个字符属于一本书,所以:
The important thing to understand is the convention by which Rails implements relationships using ActiveRecord. A book has many characters, and each character belongs to a book, so:
class Book < ActiveRecordBase
has_many :characters
end
class Character < ActiveRecordBase
belongs_to :book
end
Rails 现在假设 characters
表将有一个名为 book_id
的外键,它与 books
表相关.创建属于一本书的角色:
Rails now assumes that the characters
table will have a foreign key called book_id
, which relates to the books
table. To create a character belonging to a book:
@book = Book.new(:name=>"Book name")
@character = @book.characters.build(:name=>"Character name")
现在当保存 @book
时(假设 @book
和 @character
都有效),将在两个books
和 characters
表,其中字符行通过 book_id
链接.
Now when @book
is saved (assuming both @book
and @character
are valid), a row will be created in both the books
and the characters
tables, with the character row linked through book_id
.
要显示角色也属于用户,您可以将该关系添加到角色模型中:
To show that a character also belongs to a user, you could add that relationship to the Character model:
class Character < ActiveRecordBase
belongs_to :book
belongs_to :user
end
因此 Rails 现在期望 characters
也有名为 user_id
的外键,它指向一个 users
表(它也需要一个 用户代码>模型).创建角色时指定用户:
Thus Rails now expects characters
to also have foreign key called user_id
, which points to a users
table (which also needs a User
model). To specify the user when creating the character:
@book = Book.new(:name=>"Book name")
@character = @book.characters.build(:name=>"Character name",:user=>current_user)
也可以通过调用对象上的相应方法来分配外键:
You can also assign the foreign key by calling the corresponding method on the object:
@character.user = current_user
这一切都有效,因为它遵循 Rails 命名模型和表的约定.您可以背离这些约定,但如果这样做,您将更难学习 Rails.
This all works because it follows the Rails conventions for naming models and tables. You can depart from these conventions, but you'll have a harder time learning Rails if you do.
这篇关于Rails 3、创建主记录后如何添加关联记录(Books、Auto Add BookCharacter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!