我正在解析CSV文件并且有一个应该是布尔值的字符串列。 这些值要么是' Y'或者' N'我想在回调中处理这些并插入为boolean。这是否可以使用activerecord回调?
Rails 5.0.0
CSV:
RULE_TYPE,DESCRIPTION,STANDARD_OPTION
OY,One,Y
TN,Two,N
迁移:
...
t.boolean :standard_option
...
模型:
class OptionRule < ApplicationRecord
before_validation :standard_option_bool
private
def standard_option_bool
puts self.standard_option
self.standard_option=="Y" ? self.standard_option=true : self.standard_option=false
end
end
rails c
2.3.1 :001 > OptionRule.create(rule_type:'OY', description:"One", standard_option: 'Y')
(0.1ms) BEGIN
true
SQL (0.6ms) INSERT INTO "option_rules" ("rule_type", "description", "standard_option", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "rule_type" [["rule_type", "OY"], ["description", "One"], ["standard_option", false], ["created_at", 2016-07-25 10:34:06 UTC], ["updated_at", 2016-07-25 10:34:06 UTC]]
(6.2ms) COMMIT
=> #<OptionRule rule_type: "OY", description: "One", standard_option: false, created_at: "2016-07-25 10:34:06", updated_at: "2016-07-25 10:34:06">
请注意,它会返回&#39; true&#39;对于puts命令而不是&#39; Y&#39;。我必须认为activerecord在回调之前正在改变它。
答案 0 :(得分:1)
是的,你可以做到。即使这样也可行
class OptionRule < ApplicationRecord
before_validation :standard_option_bool
private
def standard_option_bool
self.standard_option = standard_option == "Y"
end
end
答案 1 :(得分:0)
尝试为布尔列传递 1(true)和 0(false)。
class OptionRule < ApplicationRecord
before_validation :standard_option_bool
private
def standard_option_bool
self.standard_option=="Y" ? self.standard_option = 1 : self.standard_option = 0
end
end
答案 2 :(得分:0)
请试试这个
class OptionRule < ApplicationRecord
before_validation :standard_option_bool
private
def standard_option_bool
self.standard_option = self.standard_option == "Y" ? true : false
end
end
答案 3 :(得分:0)
我通过创建一个临时变量解决了这个问题。
var m1 = new UIButton(UIButtonType.System);
m1.Frame = new RectangleF(45, -50, 230, 20);
m1.SetTitle("Beautiful That Way", UIControlState.Normal);
m1.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Beautiful That Way" );
var m2 = new UIButton(UIButtonType.System);
m2.Frame = new RectangleF(45, -20, 230, 20);
m2.SetTitle("Boys In The Sky", UIControlState.Normal);
m2.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Boys In The Sky");
var m3 = new UIButton(UIButtonType.System);
m3.Frame = new RectangleF(45, 10, 230, 20); m3.SetTitle("Cos'é l'Amor", UIControlState.Normal);
m3.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Cos'é l'Amor");
var m4 = new UIButton(UIButtonType.System);
m4.Frame = new RectangleF(45, 40, 230, 20);
m4.SetTitle("Dancing Qeen", UIControlState.Normal);
m4.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Dancing Qeen");
var m5 = new UIButton(UIButtonType.System);
m5.Frame = new RectangleF(45, 70, 230, 20);
m5.SetTitle("Free", UIControlState.Normal);
m5.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Free");
scrollView.AddSubview(m1);
scrollView.AddSubview(m2);
scrollView.AddSubview(m3);
scrollView.AddSubview(m4);
scrollView.AddSubview(m5);
然后设置OptionRule.standard_option_temp并让验证设置布尔值