我有一个来自图像标签的源 url,我想将该图像上传到我的 S3 存储桶。我该怎么做?
这工作:
foo, bar = 1, 2
foo == 1 #true
bar == 2 #true
Run Code Online (Sandbox Code Playgroud)
这也有效:
baz = true
foo = baz ? 1 : 2
foo == 1 #true
Run Code Online (Sandbox Code Playgroud)
这不起作用:
foo, bar = baz ? 1, 2 : 3, 4
# SyntaxError: (irb):4: syntax error, unexpected ',', expecting ':'
# foo, bar = baz ? 1, 2 : 3, 4
# ^
Run Code Online (Sandbox Code Playgroud)
应如何格式化以使其有效?
是否有一行简单的代码可以包含在种子文件的顶部,以便在插入新的种子数据之前清除每个表而不运行任何rake命令来回滚表或数据库?
我想的是:
[Foo, Bar].each{|class| class.destroy_all}
Run Code Online (Sandbox Code Playgroud)
关键是我想添加新数据,其中每个新插入从id开始:1.我想要避免的是删除一个包含100行的表,当我添加新数据时,它从101开始.
我有一个带有序列化文本列的模型.当我保存并重新加载记录时,值似乎仍然存在,但是当我查询记录时,值已消失.
(为简单起见,下面的演示被截断)
class Subscription < ActiveRecord::Base
serialize :pending_changes, Hash
attr_accessible :pending_changes
attr_accessor :pending_changes
end
class AddPendingChangesToSubscriptions < ActiveRecord::Migration
def change
add_column :subscriptions, :pending_changes, :text
end
end
s = Subscription.new
s.pending_changes = {foo: "bar"}
s.save
# => true
s.reload.pending_changes
# => {foo: "bar"}
Subscription.last.pending_changes
# => nil
Run Code Online (Sandbox Code Playgroud)
保存{"foo" => "bar"}为值时会发生同样的事情.
我也注意到,在save调用时,生成的SQL查询如下:
UPDATE 'subscriptions' SET 'updated_at' = '2017-12-01 23:46:05', 'pending_changes' = '--- {}\n' WHERE 'subscriptions'.'id' = 2
Run Code Online (Sandbox Code Playgroud)
根据类似问题的答案,我已经确定了db列数据类型,text并且我尝试使用serialize :column_name带有和不带附加的Hash.