Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 理解rails 3.2中控制器中的SQL代码_Ruby On Rails_Ruby_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 理解rails 3.2中控制器中的SQL代码

Ruby on rails 理解rails 3.2中控制器中的SQL代码,ruby-on-rails,ruby,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3.2,我是RubyonRails的新手,我想了解应用程序中已经存在的一些代码 代码如下(书籍):- 我发现这有点难以理解。为什么要使用此代码,为什么不使用以下代码:- def index if params[:book_id] @book = Book.find(:all, :conditions => ["book_id = ? ", params[:book_id] ], :order

我是RubyonRails的新手,我想了解应用程序中已经存在的一些代码

代码如下(书籍):-

我发现这有点难以理解。为什么要使用此代码,为什么不使用以下代码:-

def index
  if params[:book_id]
    @book = Book.find(:all,
                        :conditions => ["book_id = ? ", params[:book_id] ],
                        :order      => "action_date ASC")    
  end
end

有人能帮我一下吗。

我想你会发现本书的ActiveRecord部分非常有用。我建议仔细阅读一下这些文件。密切注意示例的总体风格。MVC(Model-View-Controller)模式最强大的一个方面是,您可以在模型中构建非常简单的接口来完成“繁重的工作”,而不是用真正不属于模型的逻辑将控制器弄得乱七八糟。

阅读关于rails中的关联和范围的手册和教程。 之后,您应该将代码重写为以下内容:

#model

class Book < ActiveRecord::Base
  # Association for BookIssue, the BookIssue model should have a 'belongs_to :book'
  has_one :book_issue
  # Association for BookHold, the BookHold model should have a 'belongs_to :book'
  has_one :book_hold
  # Scope to get not returned books, it joins all issues that don't have a return date.
  # All book with a return date will be ignored.
  scope :not_returned, joins(:book_issue).where(:book_issues => { return_date: nil } )

end

#controller

def index
  # Use the scope mentioned in the model, to get all not returned books.
  @books = Book.not_returned.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @books }
  end
end
#books controller
def index
  @books = Book.available

  respond_to ... # continue as before
end

# book model
scope :available, where(copies_available: true)

# book_issue model
belongs_to :book
after_save :update_parent_availability

def available?
  return_date.nil?
end

def update_parent_availability
  book.copies_available = book.issues.select(&:available?).any?
  book.save if book.changed?
end
#模型
类目{return_date:nil})
终止
#控制器
def索引
#使用模型中提到的范围,获取所有未归还的书籍。
@书=书。未归还。全部
回应待办事项|格式|
format.html#index.html.erb
format.json{render json:@books}
终止
终止

这里要解决的主要问题是“生成当前未签出的书籍数组,并将其传递给模板引擎以在页面上呈现”。SQL代码正在处理第一部分。不幸的是,您必须加入book_issues查看是否有任何可用副本,但忽略这一点,现在您希望在book上定义一个方法,如
:available?
,当至少有一个副本未签出时返回true,然后在控制器中使用该方法

作为进一步的调整,我希望在图书记录上有一个数据库列,让我知道它们是否可用于签出,而无需加入
图书问题
(BooksController#索引听起来会被大量调用,您不希望它重击数据库)。这可能意味着更新您的图书签出逻辑以调整主图书记录

如果代码看起来像这样,那么代码会更快乐:

#model

class Book < ActiveRecord::Base
  # Association for BookIssue, the BookIssue model should have a 'belongs_to :book'
  has_one :book_issue
  # Association for BookHold, the BookHold model should have a 'belongs_to :book'
  has_one :book_hold
  # Scope to get not returned books, it joins all issues that don't have a return date.
  # All book with a return date will be ignored.
  scope :not_returned, joins(:book_issue).where(:book_issues => { return_date: nil } )

end

#controller

def index
  # Use the scope mentioned in the model, to get all not returned books.
  @books = Book.not_returned.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @books }
  end
end
#books controller
def index
  @books = Book.available

  respond_to ... # continue as before
end

# book model
scope :available, where(copies_available: true)

# book_issue model
belongs_to :book
after_save :update_parent_availability

def available?
  return_date.nil?
end

def update_parent_availability
  book.copies_available = book.issues.select(&:available?).any?
  book.save if book.changed?
end

:更新\u父级\u可用性
操作可能受竞争条件的影响。您可能应该将其分解到helper book availability management类中,并在事务中运行它。

第二个块完全不同,它应该用于
show
操作,而不是
index
。控制器操作没有意义。你打算怎么做?@kiddorails我实际上不知道。但代码似乎适用于索引。它显示了数据库中存在的所有书籍列表。看起来索引操作试图返回当前发行的书籍和请求json格式时处于保留状态的书籍的分组计数。我们可能应该看看索引视图,看看它们在请求html时做了什么不要太武断,但SQL代码是一个巨大的危险信号。首先,它应该主要在book模型中,而不是在book控制器中,其次,有ActiveRecord查询方法可以以更惯用的方式处理其中的大部分内容。您最好重新实现这篇文章,只是为了了解它是如何工作的。