Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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
Ruby on rails 有很多和:在添加之后/:在添加之前=>;回拨<&书信电报;并创建方法_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 有很多和:在添加之后/:在添加之前=>;回拨<&书信电报;并创建方法

Ruby on rails 有很多和:在添加之后/:在添加之前=>;回拨<&书信电报;并创建方法,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,我正在阅读Rails 3-Way手册,在这一点上感到困惑: :在添加=>回调之后 通过将记录添加到集合后调用,在将项添加到集合时会触发添加前和添加后回调。它与记录是否保存到数据库无关。(此处对has\u和\u属于\u many和has\u many:through关系有一个轻微的例外,将其添加到集合中会立即由ActiveRecord内部反映到数据库中) 一旦向集合中添加新记录,回调就会触发。在将元素添加到集合之前,将调用before_add,而after_add after 下面的示例可能会让您

我正在阅读Rails 3-Way手册,在这一点上感到困惑:

:在添加=>回调之后
通过将记录添加到集合后调用,在将项添加到集合时会触发
添加前
添加后
回调。它与记录是否保存到数据库无关。(此处对
has\u和\u属于\u many
has\u many:through
关系有一个轻微的例外,将其添加到集合中会立即由ActiveRecord内部反映到数据库中)

一旦向集合中添加新记录,回调就会触发。在将元素添加到集合之前,将调用before_add,而after_add after

下面的示例可能会让您更好地理解

# id: integer
class Author < ActiveRecord::Base
  has_many :books, before_add: :bef, after_add: aft

  def bef
    puts "Before adding, author ##{id} has #{books.size} books"
  end

  def aft
    puts "After adding, author ##{id} has #{books.size} books"
  end
end

# id integer
# author_id: integer
class Book < ActiveRecord::Base
  belongs_to :author
  after_save: :saved

  def saved
    puts "The book is now saved!"
  end
end

> book = Book.new

> author = Author.first

> author.books << book
'Before adding, author #1 has 3 books'
'After adding, author #1 has 4 books'

> author.save
'The book is now saved'
#id:integer
类作者书
>author=author.first
>author.books.author.save
“这本书现在已保存”

在添加后的语句和添加前的代码中,这是一个错误吗?是问题错了还是理解了问题?我没有在Rails指南中看到这种行为的记录。也许这本书写完后这种行为已经改变了。@AbhayKumar谢谢。我已经改变了主题以匹配帖子的内容,这使得这篇文章毫无价值。当使用外键直接添加记录时,回调不会被触发。@maletor我可以为这个功能想出一些可能的用例。特别是由于
中赋值操作的破坏性,有许多:通过
/habtm关系:除了直接与参考模型接口(这会破坏:通过的目的)之外,这是您可以1的唯一方法。跟踪更改并在需要时回滚,或2。获得对记录的访问权,并在从集合中添加/删除记录之前抛出中止。特别是由于双方都没有进行验证,这比您想象的更为重要。
 > b = Book.first
  Book Load (0.1ms)  SELECT "books".* FROM "books" LIMIT 1
 > b.chapters.create(title: 'Last Chapter')
  begin transaction
chapter added to book
  INSERT INTO "chapters" ....
  commit transaction
b.chapters.new(title: 'New Chapter')
b.chapters.build(title: 'New Chapter')
# id: integer
class Author < ActiveRecord::Base
  has_many :books, before_add: :bef, after_add: aft

  def bef
    puts "Before adding, author ##{id} has #{books.size} books"
  end

  def aft
    puts "After adding, author ##{id} has #{books.size} books"
  end
end

# id integer
# author_id: integer
class Book < ActiveRecord::Base
  belongs_to :author
  after_save: :saved

  def saved
    puts "The book is now saved!"
  end
end

> book = Book.new

> author = Author.first

> author.books << book
'Before adding, author #1 has 3 books'
'After adding, author #1 has 4 books'

> author.save
'The book is now saved'