简体   繁体   English

任何人都可以详细解释这行代码的作用吗?

[英]Can anybody explain in a detail manner what this line of code does?

Idea.where("user_id = ?", id)

and also I wanted to ask, when should we pass param[:id]. 而且我想问一下,我们什么时候应该通过param [:id]。 I find it very confusing as to how where does rails find the parameters and how does this "params" work 我觉得很困惑的是rails如何找到参数以及这个“params”如何工作

It fetches all the records from the ideas table that have in their user_id column the value of the variable id . 它从ideas表中获取所有记录,这些记录在user_id列中包含变量id的值。

For example if id is 3 this would be equivalent to: 例如,如果id3 ,则相当于:

Idea.where(user_id: 3)

The ? ? is a placeholder and prevents against SQL injection attacks . 是一个占位符,可以防止SQL注入攻击

Probably provides sql query like select * from ideas where user_id = 14 . 可能提供sql查询,如select * from ideas where user_id = 14 Where 14 is the value of id variable. 其中14id变量的值。 But first returns scope object. 但首先返回范围对象。 Query will be executed when necessary. 必要时将执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM