找到 find_by 与 find 的区别
两者都是finder, 这解释一下find_by, Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself.
I. 有个说法讲二者区别为, 一个找row, 一个找column...
The 『find』 method is usually used to retrieve a row by ID『find_by』 is used as a helper when you're searching for information within a column, and it maps to such with naming conventions.
II. 两者代码格式不用, find(id) 括号内格式区别于 find_by(id: id)
III. 在数据库能找到的时候, 两者毫无区别. 但找不到需要的数据时, 返回的结果完全不同. find会显示报错, find_by会直接给你一个nil值的反馈. Both do the exact same thing when the record exists in the database. However, they handle differently the return values, when the record is not found in the database.
5月15日Vedio里面的20分钟开始有官方解释!!!
具体例子参考这网页, 自己开rails console玩一下, 实操一下超级直观
与之前的"self"一样让我困惑的 "ci"是什么?
完全没有找到购物车用到的代码ci大哥, 只好找self的一些介绍来填补内心的不平衡...
『self is a reserved keyword in Ruby that always refers to an object, but the object self refers to frequently changes based on the context.
When methods are called without an explicit receiver, Ruby sends the message to the object assigned to the self keyword. Calling methods without an explicit receiver is common, so understanding the object assigned to the keyword self at any time is essential』
看起来感觉self就是个指代名称, 因为被它指代的东西多变, 所以用self...大概这样
什么是 helper_method? 它特别在哪里? 为啥特别指出来它的种类才能调用?
The method 『helper_method』 is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers). 大概是, 让定义在controller里面的methods可以直接用到view里面. 或者说, 为了让这method即能在controller使用也能在前端view使用.
If you need to use it in both the controller and the view then write it in the controller. Then use the wonderful method named helper_method. 另一文章同样描述, 在helper_method后面被激活的(比如, 下面的current_cart)就能同时在view以及controller调用了.
为什么要添加这句代码
helper_method :current_cart
才能开始定义 "current_cart" ? 换句话说, 为啥一定要
helper_method :current_cart
def current_cart
@current_cart ||= find_cart
end
如果不明确指出current_cart是个helper_method会怎么样? 比如直接开始定义current_cart会错过啥?! 看了查到的文章们, 我推测这两问题的答案是, 如果不用helper_method激活current_cart的话, 它无法在view以及controller里都能使用.
代码末尾 blank? 的作用
有个概述版, 先稍微参考一下
『nil?』 When the value is nil for each object.
『empty?』 To check for the empty string, array, hash.
『blank?』 To check the object has a value of nil
『present?』 The opposite of "blank?"
细说『 blank?』 objects are false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.
代码末尾的 present?
An object is present if it's not blank. 跟blank是相反的效果, 这有张表格很直观
总结起来, 一个直观的例子是
!object.blank? == object.present?
session是? 类比成电子身份识别卡...
A session is just a place to store data during one request that you can read during later requests. 能类比 session 的熟悉的例子是 cookies, 但两者有区别.
session好像很强大啊, 因为『What if your Rails app couldn’t tell who was visiting it? If you had no idea that the same person requested two different pages? If all the data you stored vanished as soon as you returned a response?』这类问题涉及的数据, session都能帮忙 keep track of the certain state of a particular user.
Without the idea of sessions, the user would have to identify, and probably authenticate, on every request. 有了session就不用每次都给新数据了, 用之前存在的就好.
A session usually consists of a hash of values and a session ID, usually a 32-character string, to identify the hash. Every cookie sent to the client's browser includes the session ID. And the other way round: the browser will send it to the server on every request from the client. 真的算是"电子识别卡"来的, 好比喻!
找到一个学长姐的文章总结解释 请参考
http://james1239090-blog.logdown.com/posts/773514-rails-session-and-cookie
这边有个很全的reference, 可以认真读一下
https://guides.rubyonrails.org/action_controller_overview.html
购物车代码部分, 新建一个cart后要执行的 "return"是什么?
Your function can compute values and store them in local variables that are specific to the function. Those values can then be returned with the return statement. 将存在var里面的值显示出来.
Ruby provides a keyword that allows the developer to explicitly stop the execution flow of a method and return a specific value. 有点"break out early"的意思, 就是"别再loop啦, 停下来给个结果先"的感觉.
在routes里面的"member do"到底什么情况使用. 可以理解成"自定义routes的时候用"吗?
You may add additional routes that apply to individual members of the collection.
功能是给集合里某个member加个route. 所以自定义route时才有机会用到 member do 吧~
关联表格的时候, 后半句加上 "through: :XXX , source: :XXXX" 为什么不到第三方的model进一步定义, 这里直接提及就算定义完成?
这代码居然就是API相关代码吖, 我居然在不知道的情况下已经都用了API了呢 😂
『:through』 association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.
简要的回答提问就是, ruby自动识别不了, model间关系复杂导致不知道你到底要用哪个, 所以要手动打出 :source来描述要具体去哪个model获取相关数据.
这里有官方对于"关联"的解释文章, 困惑的时候推荐读. 英文是"association", 用来关联model用的. 为什么需要关联呢? 文章一开篇就解释的很赞
https://guides.rubyonrails.org/association_basics.html
前端代码 link_to系列, 这 "link_to XXX_path do" 以及 " link_to ("XXX", XXX_path)" 两者区别为何?
首先, 常常用的link_to居然是个helper method来的!
有『do』出现一般是用在有"image"的情况下, an image tag or a span with an icon inside it (或者其他复杂的前端呈现代码), 教程中的情况是属于 using click-able images. 说是pass the image in a block if you like that style better的情况, 即, 用带 do 的代码, 代码会比较美观.
两者区别为何 "thumbnail" 以及 "thumb"?
看了一些资料后, 有个感觉就是这两是在Gem里才有的代码哈哈哈哈, 所以稍微通过平常的实例进行感悟一下即可. 有用的资料也有限, 就不赘述了.
定义的名称跟使用时不同, 却不会报错?
定义的时候起名叫
render_cart_total_price(cart)
但使用的时候却用的是
render_cart_total_price(current_cart)
前面全部一致, 但是括号里面的内容不同啊, 这样怎么也能正常使用不报错的?!
这个问题嘛...估计要多遇到点例子会懂吧? 因为这关键词也不知查什么...先把这个问题记录起来, 未来的我自己能回答的.
什么是 ||= ?
例子
a ||= b
就是当a的值是nil的时候, 就会将b的值当做自己的值. 但如果a有自己的值的话, b就连出场的机会也没有, 就是路人一个.
网友评论