我当前的逻辑是,从表单中获取两个输入,其中一个是主账簿,一个是辅助账簿,并将其保存到数据库中,其中is_primary check对于主账簿为true,对于辅助账簿为false。我们可以有多本二手书。
我有一张桌子上的书:表名:书籍
- book_id
- book_name
我还有另一位表作者:表名:作者
- author_id
- author_name
我有如下关系表:表名:author_book
- author_book_id
- book_id
- author_id
- is_primary(布尔(
表单代码:
<div class="elem-set">
<div class="elem-label">
<label>Author</label>
</div>
<%= f.select :associated_author,
Author.sort_by_name.map{|t| ["#{t.id}: #{t.name}", t.name]},
{ include_blank: "No Author" },
{ class: 'chzn-select', multiple: false }
%><br /><br />
</div>
<div class="elem-set">
<div class="elem-label">
<label>Primary Book</label>
</div>
<%= f.select :associated_primary_book,
Book.sort_by_name.map{|n| ["#{n.book_name}: #{n.book_id}", n.book_name]},
{ include_blank: "No Book" },
{ class: 'chzn-select', multiple: false }
%><br /><br />
</div>
<div class="elem-set">
<div class="elem-label">
<label>Secondary Book</label>
</div>
<%= f.select :associated_secondary_book,
Book.sort_by_name.map{|n| ["#{n.book_name}: #{n.book_id}", n.book_name]},
{ include_blank: "No Book" },
{ class: 'chzn-select', multiple: true }
%><br /><br />
</div>
型号图书类别:
class Book < ActiveRecord::Base
has_many :author_book, dependent: :destroy
has_many :authors, through: :author_book
scope :sort_by_name, -> { select([:book_id, :book_name]).order('book_name ASC') }
end
作者类别:
class Author < ActiveRecord::Base
has_many :author_book, dependent: :destroy
has_many :books, through: :author_book
def associated_primary_books=(fetched_books)
self.books.clear
if !fetched_books.empty?
self.books << Book.find_by_name(fetched_books)
#is_primary=> true
end
end
def associated_secondary_books=(fetched_books)
fetched_books = fetched_books.delete_if { |x| x.empty? }
if !fetched_books.empty?
fetched_books.each do |book|
self.books << Book.find_by_name(book)
end
end
end
end
AuthorBook类:
class AuthorBook < ActiveRecord::Base
belongs_to :authors
belongs_to :books
end
控制器作者控制器
AuthorController < ApplicationController
def create
@Author = Author.new(params[:author])
respond_to do |format|
begin
retries ||= 0
if @author.save
flash[:notice] = 'Author created.'
Event.init(user: current_user.id, type: "Create Author", details: {
:author => @author.id
})
format.html { redirect_to action: 'show',
:id => @author.id
}
end
rescue ActiveRecord::StatementInvalid => e
flash[:error] = 'error'
redirect_to action: 'new'
end
end
end
ISSUE:我想要实现的是,每当我保存book对象时,我也会以某种方式将author_book表中的is_primary
保存为true或false。
- 当我尝试将书添加到作者时,它被成功保存。但我无法更新is_primary属性,导致作为次要书籍。如果我尝试更新,我会得到未定义的方法错误is_primary属性。如何访问is_primary属性哪个存在于关系表中并干净地解决了这个问题
- 如果我在添加book后尝试更新author_book表,它会返回空数组,因为在author创建
- 这是我创建的带有示例的存根代码,而不是完整的代码
我们使用的是Rails 3。
基本上,您想在这里做的是循环并追加记录。除了Rails 3不支持刚刚在Rails 6中添加的追加部分。你能做的是:
class Author < ActiveRecord::Base
has_many :author_books, dependent: :destroy
has_many :books, through: :author_books
def associated_primary_books=(fetched_books)
fetched_books.select(&:present?).each do |id|
ab = author_books.find_or_initialize_by(book_id: id)
ab.is_primary = true
ab.save unless new_record?
end
end
def associated_secondary_books=(fetched_books)
fetched_books.select(&:present?).each do |id|
ab = author_books.find_or_initialize_by(book_id: id)
ab.is_primary = false
ab.save unless new_record?
end
end
end
请注意,我使用的是这本书的id而不是名字。不要把名字传来传去——这很愚蠢,因为书名几乎不是唯一的,所以歧义肯定会发生。