当假设记录可能不存在时,Ruby on Rails 可以防止 nil 错误

发布于 2024-08-05 10:54:23 字数 745 浏览 8 评论 0原文

我正在构建一个简单的图书借阅应用程序。我需要做的事情之一是确定一本书是否被借出。我通过 book_check_out 类设置我的人员和图书类之间的关联。我的目标是使用 book 的 check_out 属性来确定一本书当前是否已签出。但是,在我目前的实现中,当一本书未签出并且我引用 book.checked_out.XXX 时,我收到错误“当您没有预料到时,您有一个 nil 对象!”我的目标是将 book.checked_out 用于两个目的,在某些视图中显示是的,该书已签出,而在其他视图中显示当前已签出给谁。

class Person < ActiveRecord::Base
  has_many :book_check_outs
  has_many :books, :through => :book_check_outs
end

class Book < ActiveRecord::Base

  has_many :book_check_outs
  has_many :people, :through => :book_check_outs

  def checked_out
    book_check_outs || false
  end
end

class BookCheckOut < ActiveRecord::Base
  belongs_to :book
  belongs_to :person
end

I am building a simple book check out application. One of the things that I need to do is determine if a book is checked out. I have my associations between my people and book classes setup through a book_check_out class. My goal is to use the checked_out property of book to determine if a book is presently checked out. However, in my present implementation when a book is not checked out and I reference book.checked_out.XXX I receieve the error "You have a nil object when you didn't expect it!" My goal is to use book.checked_out for two purposes in some views show that yes, that book is checked out and in other views show who it is presently checked out to.

class Person < ActiveRecord::Base
  has_many :book_check_outs
  has_many :books, :through => :book_check_outs
end

class Book < ActiveRecord::Base

  has_many :book_check_outs
  has_many :people, :through => :book_check_outs

  def checked_out
    book_check_outs || false
  end
end

class BookCheckOut < ActiveRecord::Base
  belongs_to :book
  belongs_to :person
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-08-12 10:54:23

当你得到一个布尔结果时,可以是真也可以是假(例如,一本书可以被签出或不被签出),你需要假设结果的默认值。在这种情况下,我们可以假设它是假的(默认情况下这本书没有被签出)。

在书籍模型中,删除 has_many :checked_out 行并创建一个具有相同名称的方法:

def checked_out
  book_check_outs || false
end

应该返回 BookCheckOuts,或者如果没有与实例关联的内容, 错误的。它为您提供了一种快速、简单、万无一失的方法来检查关联并消除讨厌的 nil 对象错误。

编辑 您也可以只返回 TRUE 或 FALSE,而不通过检查 book_check_outs 是否为 nil 来返回最近的结帐。 Rails提供了一个叫blank的方法?这两个值都为零?和空?

def checked_out
  !book_check_outs.blank?
end

我爱鲁比:)

When you've got a boolean outcome, something that can either be true or false (for instance, a book can either be checked out or not), you need to assume a default for the outcome. In this case, we can assume it's false (the book is not checked out by default).

In the book model, remove your has_many :checked_out line and create a method with the same name:

def checked_out
  book_check_outs || false
end

This should return either the BookCheckOuts, or if there are none associated with the instance, FALSE. It gives you a quick, easy, foolproof method of checking an association and removing the nasty nil object error.

Edit You could also just return TRUE or FALSE, and not return the most recent checkouts by checking that book_check_outs is nil or not. Rails provides a method called blank? which calls both nil? and empty?

def checked_out
  !book_check_outs.blank?
end

I love Ruby :)

帅气尐潴 2024-08-12 10:54:23

我忘记它是否真的重要,但为了清楚起见,您可能希望将 has_many 关系放在 has_many :through 关系之上。

我也不确定您应该在 Book 类中声明它 has_many :BookCheckOuts 和 has_one :checked_out,因为 check_out 实际上 是一个 BookCheckOut,您声明它具有上述许多内容。

I forget if it actually matters or not, but for clarity's sake you might want to put the has_many relation above the has_many :through relation.

I am also not sure you should be declaring in the Book class that it has_many :BookCheckOuts AND it has_one :checked_out, since the checked_out actually is a BookCheckOut which you declared it having many of above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文