Rails - 添加不在模型中的属性并更新模型属性
我的表单中有 3 个字段不在我的数据库中:opening_type、opening_hours、opening_minutes。我想用这 3 个字段更新主要属性“opening”(在数据库中)。
我尝试了很多不起作用的事情。
实际上我有:
attr_accessor :opening_type, :opening_hours, :opening_minutes
def opening_type=(opening_type)
end
def opening_type
opening_type = opening.split("-")[0] if !opening.blank?
end
def opening_hours=(opening_hours)
end
def opening_hours
opening_hours = opening.split("-")[1] if !opening.blank?
end
def opening_minutes=(opening_minutes)
end
def opening_minutes
opening_minutes = opening.split("-")[2] if !opening.blank?
end
我尝试添加类似的内容:
def opening=(opening)
logger.info "WRITE"
if !opening_type.blank? and !opening_hours.blank? and opening_minutes.blank?
opening = ""
opening << opening_type if !opening_type.blank?
opening << "-"
opening << opening_hours if !opening_hours.blank?
opening << "-"
opening << opening_minutes if !opening_minutes.blank?
end
write_attribute(:opening, opening)
end
def opening
read_attribute(:opening)
end
但是,不会调用访问器方法,并且我认为如果调用了访问器,那么 opening_type、opening_hours、opening_minutes 也是空的...
我认为我不需要 before_save 回调,应该这样做这重写了访问器。
笔记: - 轨道 3.0.5, - opening_type,:opening_hours,:opening_minutes 可能为空
编辑:我更新了我的代码
I have 3 fields in my form witch are not in my database: opening_type, opening_hours, opening_minutes. I want to update the main attribute "opening" (in database) with these 3 fields.
I tried lot of things that doesn't work.
Actually I have:
attr_accessor :opening_type, :opening_hours, :opening_minutes
def opening_type=(opening_type)
end
def opening_type
opening_type = opening.split("-")[0] if !opening.blank?
end
def opening_hours=(opening_hours)
end
def opening_hours
opening_hours = opening.split("-")[1] if !opening.blank?
end
def opening_minutes=(opening_minutes)
end
def opening_minutes
opening_minutes = opening.split("-")[2] if !opening.blank?
end
I tried adding something like:
def opening=(opening)
logger.info "WRITE"
if !opening_type.blank? and !opening_hours.blank? and opening_minutes.blank?
opening = ""
opening << opening_type if !opening_type.blank?
opening << "-"
opening << opening_hours if !opening_hours.blank?
opening << "-"
opening << opening_minutes if !opening_minutes.blank?
end
write_attribute(:opening, opening)
end
def opening
read_attribute(:opening)
end
But, the accessors methods are not called and I think opening_type, opening_hours, opening_minutes were empty too if the accessors were called...
I think I don't need a before_save callback and should do this rewriting the accessors.
Notes:
- Rails 3.0.5,
- opening_type, :opening_hours, :opening_minutes could be empty
EDIT: I updated my code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,
attr_reader
、attr_writer
和attr_accessor
只是用于定义您自己的方法的宏。目前,您的 setter 方法没有做任何特殊的事情,因此如果您只是切换到 attr_accessor ,您的代码将变得更干净。
您的另一个问题是您的
opening=
方法永远不会被调用,这是有道理的,因为您的代码中没有任何地方调用它。你真正想要的是在所有单独的部分都设置好之后设置你的开口。现在没有简单的方法可以做到这一点,但 Rails 确实有一个before_validation
回调,您可以在其中放置在设置值之后但在验证运行之前运行的代码:Note that
attr_reader
,attr_writer
andattr_accessor
are just macros for defining your own methods.At the moment, your setter methods aren't doing anything special, so if you just switch to
attr_accessor
your code will become cleaner.Your other issue is that your
opening=
method is never being called, and this makes sense because there's nowhere in your code calling it. What you really want is for your opening to be set after all of the individual parts have been set. Now there's no trivial way to do this, but Rails does have abefore_validation
callback where you can put code that runs after values have been set but before the validation runs:而不是
你需要
hf...
// :opening_type, :opening_hours, :opening_minutes 是真实字段吗?如果是,那么您只需要这个?
attr_accessor:打开
attr_reader :打开
instead of
you need
hf...
// Are :opening_type, :opening_hours, :opening_minutes real fields? If yes then you just need this?
attr_accessor :opening
attr_reader :opening