Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在Rails活动记录中保存对象时更新相关属性_Mysql_Ruby On Rails_Ruby On Rails 3_Design Patterns_Activerecord - Fatal编程技术网

Mysql 如何在Rails活动记录中保存对象时更新相关属性

Mysql 如何在Rails活动记录中保存对象时更新相关属性,mysql,ruby-on-rails,ruby-on-rails-3,design-patterns,activerecord,Mysql,Ruby On Rails,Ruby On Rails 3,Design Patterns,Activerecord,我当前的逻辑是,从表单中获取两个输入,其中一个是主书本,另一个是辅助书本,并将其保存到数据库中,主书本的is_primary check为true,辅助书本的is_primary check为false。我们可以有多本中学课本 我有一张桌子书: 表名:图书 书号 书名 我还有另外一张表格: 表名:作者 作者id 作者姓名 我的关系表如下: 表名:作者书 作者(书号) 书号 作者id is_小学(Bool) 表格编号: <div class="elem-set&qu

我当前的逻辑是,从表单中获取两个输入,其中一个是主书本,另一个是辅助书本,并将其保存到数据库中,主书本的is_primary check为true,辅助书本的is_primary check为false。我们可以有多本中学课本

我有一张桌子书: 表名:图书

  • 书号
  • 书名
我还有另外一张表格: 表名:作者

  • 作者id
  • 作者姓名
我的关系表如下: 表名:作者书

  • 作者(书号)
  • 书号
  • 作者id
  • is_小学(Bool)
表格编号:

  <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 AuthorBook < ActiveRecord::Base
  belongs_to :authors
  belongs_to :books
  
end
classbook{select([:book\u id,:book\u name]).order('book\u name ASC')}
结束
作者类别:

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
类作者@author.id
}
结束
rescue ActiveRecord::语句无效=>e
闪存[:错误]=“错误”
将_重定向到操作:“新建”
结束
结束
结束
问题:我想要实现的是,每当我保存book对象时,我也会以某种方式将author\u book表中的
is\u primary
保存为true或false

  • 当我尝试将书添加到作者时,它被成功保存。但是我无法更新is_primary属性,导致 书籍是次要的。如果尝试更新,则会出现未定义的方法错误 是_的主要属性。如何访问is_primary属性 关系表中存在哪一个并干净地解决此问题
  • 如果在添加book后尝试更新author_book表,它将返回空数组,因为所有内容都是在author完成后保存的 创造
  • 这是我创建的示例的存根代码,不是完整的代码

  • 我们使用的是Rails 3。

    基本上您在这里要做的是循环并插入记录。除了Rails 3不支持刚添加到Rails 6中的upserts之外。您可以做的是:

    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而不是名称。不要到处传名字——这很愚蠢,因为书名很难是唯一的,所以必然会产生歧义。

    您的表应该命名为
    作者书籍
    ,并且关联
    有很多:作者书籍
    。我有点惊讶,这还没有导致错误。您好,max,我收到find_或initialize_by的错误,因为在调用关联的_primary_books之后,author的记录被插入到author controller中。据我观察,关联的_primary_books在self.books中分配books对象,一旦在Author控制器中调用@Author=Author.new(params[:Author]),就会将其保存到数据库中。有什么好主意吗?我想把这段代码移到Author.new(params[:Author])下面,但看起来一点也不好。我得到的错误是:未定义[]:ActiveRecord::Relation的方法“find_或_initialize_by”
    
    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