如何通过 Ruby 和 mysql2 在 Sequel Pro 中创建/维护 ID 字段 [英] How to create/maintain ID field in Sequel Pro via Ruby and mysql2
问题描述
在 Sequel Pro 中,使用以下语句创建了一个表:
In Sequel Pro, created a table using this statement:
CREATE TABLE dogs(
id INT PRIMARY KEY AUTO_INCREMENT,
name TEXT,
color TEXT
);
由于 id
值是由 MySQL 创建的,我想在 ruby 中设置/创建/维护 id
字段,以便 puts dog.id
不会是空字符串.
Since the id
value is being created by MySQL, I would like to set/create/maintain the id
field in ruby so that puts dog.id
will not be an empty string.
想知道这段代码是否可以实现:
Was wondering if this piece of code would accomplish that:
def new_from_hash(hash)
Dog.new(hash["name"], hash["color"]).tap {|dog| dog.id = hash["id"]}
end
注意:我不是在跑导轨;只是普通的 ruby、gem mysql2 和 Sequel Pro.
NOTE: I am not running rails; just plain ruby, the gem mysql2, and Sequel Pro.
谢谢!
推荐答案
好吧,我相信我终于想通了.
Okay, I believe I finally figured this out.
由于 id 值是由 MySQL 创建的,我想在 ruby 中设置/创建/维护 id 字段,以便 puts dog.id
不会是空字符串."
"Since the id value is being created by MySQL, I would like to set/create/maintain the id field in ruby so that puts dog.id
will not be an empty string."
=> 因为 id 值是在 MySQL 中自动分配的,Ruby 无法知道该 id 值是什么,因此调用 dog.id 将返回一个空字符串.
=> because id value is automatically assigned in MySQL Ruby has no way of knowing what that id value is and therefore calling dog.id will return an empty string.
=> 我需要找到一种方法来在 ruby 中映射对象的 id 值,以便它与我将对象插入 MySQL 数据库时自动分配给对象的 id 值相同.
=> I need to find a way to map an objects' id value in ruby such that it is the same as the id automatically assigned to the object when I insert it in MySQL database.
=> 注意创建桌狗的语句:
=> Pay attention to the statement used to create Table dogs:
CREATE TABLE dogs(
id INT PRIMARY KEY AUTO_INCREMENT,
name TEXT,
color TEXT
);
它转换为每个狗创建都有一个 id(自动分配给它)、一个名称和一个颜色.考虑存储狗对象的信息有两种方法:
it translates to each dog create has an id (which is automatically assigned to it), a name, and a color. There are two approaches to thinking of the storage of a dog objects' information:
dog = { "id" => 'some integer', "name" => "simba", "color" => "grey" }
dog = [1, "simba", "grey"]
我通常喜欢使用数组来存储信息,但这次我使用了哈希(因为当我对狗(不是狗)调用 .inspect 时,结果是这样的:
I typically like to use arrays to store information but this time around I used a hash (because when I call .inspect on dog (not Dog) the result is something like this:
#<Dog:0x007fbf74c55068 @name="samba", @color="grey">
这让我想到了一个哈希数据结构:
which makes me think of a hash data structure:
{ "id"=> 1, "name"=>"simba", "color"=>"grey"}
在 Ruby 中,我这样做:
In Ruby, I do this:
def row_hash(hash)
hash={}
Dog.new(hash[name], hash[color]).tap { |id| id = hash[id] }
end
其中 hash_row 是指保存每个狗对象属性的行.这基本上让 Ruby 知道每次实例化 Dog 类的新实例时,它应该利用该实例化并将 hash[id] 映射到id".
Where hash_row refers to the row that holds each dog object's attribute. This basically lets Ruby know that each time it instantiates a new instance of the class Dog it should tap into that instantiation and map hash[id] to "id".
这样做允许我访问 ruby 中id"的值.
Doing this allows me to access the value of "id" in ruby.
PS:这只是我突然想到的.我可能会在它渗透一段时间后编辑这个答案.
PS: This just occurred to me. I probably will edit this answer after it percolates for a while.
这篇关于如何通过 Ruby 和 mysql2 在 Sequel Pro 中创建/维护 ID 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!