我觉得这需要重构 - 有帮助吗?红宝石建模
因此,假设您有
line_items
和 line_items 属于一个品牌和一个模型
一个品牌有许多模型和行项目
一个模型属于一个模型
对于简单的示例想法 LineItem.new(:make => "Apple", :model = >“Mac Book Pro”)
创建 LinteItem 时,您需要一个用于品牌和型号的文本字段框。品牌和型号不应多次存在。
所以我使用了以下实现:
before_save :find_or_create_make, :if => Proc.new {|line_item| line_item.make_title.present? }
before_save :find_or_create_model
def find_or_create_make
make = Make.find_or_create_by_title(self.make_title)
self.make = make
end
def find_or_create_model
model = Model.find_or_create_by_title(self.model_title) {|u| u.make = self.make}
self.model = model
end
但是使用此方法意味着我必须运行自定义验证而不是 #validates_presence_of :make 由于关联发生在虚拟属性
validate :require_make_or_make_title, :require_model_or_model_title
def require_make_or_make_title
errors.add_to_base("Must enter a make") unless (self.make || self.make_title)
end
def require_model_or_model_title
errors.add_to_base("Must enter a model") unless (self.model || self.model_title)
end
Meh 上,这开始很糟糕。现在,真正糟糕的是使用表单进行编辑。考虑到我的表单字段是部分的,我的编辑正在呈现与新表单相同的表单。这意味着 :make_title 和 :model_title 在表单上为空白。
我不太确定纠正上述问题的最佳方法是什么,这是我认为需要完全重构的最后转折点。
如果有人可以提供任何反馈,那就太好了。
谢谢!
So let's say you have
line_items
and line_items belong to a make and a model
a make has many models and line items
a model belongs to a make
For the bare example idea LineItem.new(:make => "Apple", :model => "Mac Book Pro")
When creating a LinteItem you want a text_field box for a make and a model. Makes and models shouldn't exist more than once.
So I used the following implementation:
before_save :find_or_create_make, :if => Proc.new {|line_item| line_item.make_title.present? }
before_save :find_or_create_model
def find_or_create_make
make = Make.find_or_create_by_title(self.make_title)
self.make = make
end
def find_or_create_model
model = Model.find_or_create_by_title(self.model_title) {|u| u.make = self.make}
self.model = model
end
However using this method means I have to run custom validations instead of a #validates_presence_of :make due to the associations happening off a virtual attribute
validate :require_make_or_make_title, :require_model_or_model_title
def require_make_or_make_title
errors.add_to_base("Must enter a make") unless (self.make || self.make_title)
end
def require_model_or_model_title
errors.add_to_base("Must enter a model") unless (self.model || self.model_title)
end
Meh, this is starting to suck. Now where it really sucks is editing with forms. Considering my form fields are a partial, my edit is rendering the same form as new. This means that :make_title and :model_title are blank on the form.
I'm not really sure what the best way to rectify the immediately above problem is, which was the final turning point on me thinking this needs to be refactored entirely.
If anyone can provide any feedback that would be great.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 line_items 不应该属于某个品牌,它们应该只属于一个型号。一个模型应该有很多行项目。一个品牌可以通过一个模型拥有许多行项目。您缺少一些显示字段的方法。
这实际上并没有那么糟糕,只是没有超级简单的方法来做到这一点。我希望您在某个时候在这些文本字段上添加自动完成功能。
I don't think line_items should belong to a make, they should only belong to a model. And a model should have many line items. A make could have many line items through a model. You are missing a couple of methods to have your fields appear.
It's really not that bad, there's just not super easy way to do it. I hope you put an autocomplete on those text fields at some point.