当枚举字段设置为无效值时,有没有办法使MySQL默认为NULL?
另外,有没有办法可以在HTML表单中指定一个值,这样当保存到DB时,它会存储为NULL.我不想做任何检查,如"if empty string,然后保存NULL".如果有可能,这是个好主意吗?任何安全问题?
我的场景:我有一个HTML下拉菜单,其中包含一个"null"选项,后跟一个与DB的枚举对应的项目列表.当选择并保存null选项时,我希望它在DB中反映为NULL.现在,它被转换为空字符串,如下所示:
如果在ENUM中插入无效值(即,允许值列表中不存在的字符串),则会插入空字符串作为特殊错误值.通过此字符串具有数字值0的事实,可以将此字符串与"正常"空字符串区分开.
我在ActiveRecord模型类中有这个类级方法.
def self.is_animal_color_correct?(animal, color)
if AnimalColor.find_by_animal_and_color(animal.downcase, color.downcase)
true
else
false
end
end
Run Code Online (Sandbox Code Playgroud)
我只是想知道格式化方法的最佳方法是什么.这看起来很冗长,但很清楚.
如果我在Ruby中有一个数组,就像这样:
["foo", "bar", "bat"]
Run Code Online (Sandbox Code Playgroud)
如何使用每个值组合生成新数组?
我需要输出看起来像这样:
["foo", "bar", "bat", "foo-bar", "foo-bat", "bar-bat", "foo-bar-bat"]
Run Code Online (Sandbox Code Playgroud)
订单并不重要.另外,我并不需要两个 "foo-bar"
和"bar-foo"
.
原始阵列最多可包含5-6个成员.
我正在使用这个JQuery UI组件:
http://docs.jquery.com/UI/Effects/Transfer
我的代码是这样的:
$(".button").click(function () {
var source = $(this).attr("src");
$(".ui-effects-transfer").css("background-image", "url("+source+")");
$(this).effect("transfer", { to: $(".target") }, 1000);
});
Run Code Online (Sandbox Code Playgroud)
但是这一行不会影响传输脚本:
$(".ui-effects-transfer").css("background-image", "url("+source+")");
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
在我正在使用的宝石中,我找到了片段:
@object.with(&block)
但该方法with(&block)
未在项目中定义.看起来它被定义为Ruby somwhere中的基本方法,但我不确定.
这是什么意思?有人可以指出定义该方法的位置(比如Object或Class或其他一些Ruby类)?
编辑:
有问题的代码:
def self.redis(&block)
raise ArgumentError, "requires a block" if !block
@redis ||= Sidekiq::RedisConnection.create(@hash || {})
@redis.with(&block)
end
Run Code Online (Sandbox Code Playgroud)
它来自Sidekiq项目(https://github.com/mperham/sidekiq).该项目还包括redis-rb
gem(https://github.com/redis/redis-rb).我找不到任何一种with
方法.
也许我只是缺少一些东西.
我正在为两个不同的项目进行两种不同的导轨安装.它们位于不同版本的rails上.这是我安装的:
gem list --local | grep rails
表明我安装了这个:
rails (3.0.0.beta4, 2.3.5)
当我运行命令为使用2.3.5的应用程序进行部署时,我收到以下错误:
Missing the Rails 2.3.5 gem. Please `gem install -v=2.3.5 rails`, update your
RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do
have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.
Run Code Online (Sandbox Code Playgroud)
它没有找到正确的rails版本,即使我安装了它.这个问题的短期和长期解决方案有哪些好处?
我有一个接收字符串数组的方法,我需要创建具有适当名称的对象.
例如:
public class temp {
public static void main(String[] args){
String[] a=new String[3];
a[0]="first";
a[1]="second";
a[2]="third";
createObjects(a);
}
public static void createObjects(String[] s)
{
//I want to have integers with same names
int s[0],s[1],s[2];
}
}
Run Code Online (Sandbox Code Playgroud)
如果我收到("一个","两个")我必须创建:
Object one;
Object two;
Run Code Online (Sandbox Code Playgroud)
如果我收到("男孩","女孩"),我必须创建:
Object boy;
Object girl;
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.