最大关联记录数
我希望模型上有最大数量的关联记录。 例如,一个项目有很多任务,但不超过二十个。
我如何执行此规则?
到目前为止我能想到的唯一解决方案是 INSERT INTO...SELECT
查询如下:
INSERT INTO
tasks (`id`,`project_id`,`title`,`body`)
SELECT
NULL, ?, ?, ?
FROM
tasks
HAVING
count(id) < MAX_NUMBER_OF_TASKS
LIMIT 1;
- 据我所知,这将保证插入最大数量的任务。我的说法正确吗?
- 有没有“Rails 方式”来做到这一点?
- 是否可以覆盖 ActiveRecord/Task 模型,以便它使用上面的查询 插入新记录?
我目前正在使用 ActiveRecord::Base.connection
的自定义方法 并在 new_record? 时调用它而不是
。.create
或 .save
? == true
I'd like to have a maximum number of associated records on a model.
E.g. a project has_many tasks, but not more then twenty.
How can I enforce this rule?
The only solution that I've been able to come up with so far is anINSERT INTO...SELECT
query like this:
INSERT INTO
tasks (`id`,`project_id`,`title`,`body`)
SELECT
NULL, ?, ?, ?
FROM
tasks
HAVING
count(id) < MAX_NUMBER_OF_TASKS
LIMIT 1;
- As far as I can tell, this will guarantee a maximum number of tasks being inserted. Am I correct in this?
- Is there a 'Rails way' to do this?
- Is it possible to override ActiveRecord/the Task model so that it uses the query above
to insert a new record?
I'm currently using a custom method with ActiveRecord::Base.connection
and calling that instead of .create
or .save
when new_record? == true
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没能尝试这个,但我不明白为什么它不起作用。
第一步:在父对象上定义一个验证器(这是一个简单的实现 - 可以/应该变得更通用):
第二步:从任务中打开项目验证:
我认为你应该开始做生意。当您尝试保存新任务时,它将验证关联的项目,如果(现在)关联的任务超过 20 个,则验证将失败。
如果你想让它更通用,你可以这样做:
I haven't been able to try this, but I can't see why it shouldn't work.
Step one: Define a validator on the parent object (this is a simple implementation - could/should be made more generic):
Step two: Turn on validation of project from tasks:
And I think you should be in business. When you try and save a new task, it'll validate the associated project, and the validation will fail if there are (now) more than 20 tasks associated.
Just in case you fancy making this more generic, you could do something like:
也许您可以向模型添加一些预保存验证,以检查它已有的关联模型数量,并在超过最大关联数时抛出验证错误。
May be you can add some pre save validation to your model, that checks how many associated models it already have, and throw a validation error if it exceeds your max number of associations.