购物网 5.8作业

找到 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玩一下, 实操一下超级直观

https://www.aloucaslabs.com/miniposts/what-is-the-difference-between-findid-and-find_byid-id-in-rails


与之前的"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就连出场的机会也没有, 就是路人一个.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,490评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,581评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,830评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,957评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,974评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,754评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,464评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,847评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,995评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,137评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,819评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,482评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,023评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,149评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,409评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,086评论 2 355

推荐阅读更多精彩内容