ruby – 如何将Mongoid foreign_key保存为整数或保持父模型具有I
发布时间:2020-12-16 19:31:48 所属栏目:百科 来源:网络整理
导读:这是情况. user embed_one profileprofile belongs_to city 我已经填写了一张城市桌 id as Integername as String 现在我在做 user.update_attributes(:profile_attributes {:city_id =“5”})模拟浏览器表单提交.然后我检查user.profile我看到city_id存储
这是情况.
user embed_one profile profile belongs_to city 我已经填写了一张城市桌 id as Integer name as String 现在我在做 我想知道在这里做什么是正确的事情.我应该让我的城市id是字符串还是BSON对象?或者我应该试图拦截update_attributes使mongoid store city_id为整数?我使用Integer作为城市的id的原因是因为我认为通过Integer搜索比搜索字符串要快.还有我有州和城市表,我想以可预测的方式匹配id??s,所以我不想使用BSON randome密钥. 解决方法
当然,如果你使用Mongoid,正确的方法是使用BSON对象作为ids.但是,如果您绝对需要使用整数作为城市ID,则可以使用这样的代码来模拟belongs_to
class Profile def city City.where(:id => self.city_id).last end def city=(new_city) self.city_id = new_city.id end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |