activerecord

Change or update an attribute value during Rails ActiveRecord validation

扶醉桌前 提交于 2020-08-27 05:58:05
问题 Summary : I'm trying to alter an attribute's value within a custom ActiveModel::EachValidator validator. Given the following prototype: def validate_each(record, attribute, value) trying to set value = thing doesn't appear to do anything -- am I missing something? There should be a smart way to do this... Detail : I accept a URL input as part of a site. I don't want to just take the URL and directly validate that it returns a 200 OK message, because that would ignore entries that didn't start

Rails Activerecord/Postgres time format

有些话、适合烂在心里 提交于 2020-08-27 05:56:39
问题 I am working on a Rails project using a Postgres database. For one of my models, I have a time column, called (cleverly) time. When I created the model, I set the data type for this column as 'time', with the (perhaps incorrect) understanding that this data type was for storing time only, no date. t.time :time However, when I submit data to the model, the time is correct but prefixed by an incorrect date: time: "2000-01-01 16:57:19" I just want the column to store the time (like '16:57:19').

Python高效开发之Django实战,你知道其中的核心思想吗?

谁说我不能喝 提交于 2020-08-11 21:59:54
前言 Django作为一个庞大的、自带电池的、整体Web开发解决方案框架,源代码多、子系统多、工具多。要将如此多的内容集成到一起,必然需要一个指导性的设计理念和哲学思维。这样才不至于显得东拼西凑、杂乱无章、接口混乱,而是整体一致、思路清晰、逻辑合理。既方便了源码开发,也方便了应用开发。 下面就介绍一下Django的设计理念和哲学思维,这其中有一些是Django源代码中正在遵循的,一些是使用者开发项目过程中需要遵循的: 系统性原则 松耦合 Django 追求各子系统(层)的低耦合和高内聚。各层之间保持代码独立、功能独立、尽量没有交联。 例如,模板层不需要知道用户的 Web 请求具体情况,模型层不需要了解模板层是如何展示数据的,视图层也不关心程序员所使用的模板系统到底是哪种和怎么使用的。通俗地说,模型层只关心数据的CRUD,视图层只负责业务逻辑的实现,模板层只管前端页面的渲染和展示。这三个核心层之间只有数据的传递,没有代码的交互,各自相对独立。 更少的代码 Django 建议每个APP的代码应该尽可能地精简,应该充分利用 Python 的动态能力,比如自省机制(introspection)。 快速开发 Django诞生于一个新闻编辑社,其应用环境要求快速开发和迅速迭代,所以在设计之初就追求以更快的速度实现需求的处理,你只需要编写一些新代码,或者修改一些局部代码就可以实现新的站点。

Store Join Model Data in Rails [duplicate]

心已入冬 提交于 2020-08-10 13:02:50
问题 This question already has answers here : Rails HABTM setup, model object, & join_table insertion controller setup (2 answers) Closed 13 days ago . I'm new to Ruby on Rails, and I'm developing a backend API. Currently, I got 2 Active Record Models called Book and Genre . Active Record Model class Book < ActiveRecord::Base has_and_belongs_to_many :genres end class Genre < ActiveRecord::Base hast_and_belongs_to_many :books end DB Schema Model create_table :books do |t| t.string "title" end

springboot+mybatis-plue实现内置的CRUD使用详情

喜欢而已 提交于 2020-08-07 04:46:16
springboot+mybatis-plue实现内置的CRUD使用详情,具体修改删除操作内容后文也有详细说明 mybatis-plus的特性 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑 损耗小:启动即会自动注入基本CURD,性能基本无损耗,直接面向对象操作 强大的 CRUD操作:内置通用 Mapper、通用Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 支持 Lambda形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错 支持主键自动生成:支持多达 4种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题 支持 ActiveRecord 模式:支持ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere ) 内置代码生成器:采用代码或者Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通

小书MybatisPlus第5篇-Active Record模式精讲

别说谁变了你拦得住时间么 提交于 2020-07-28 17:18:49
本文为一个系列中的第五节,前四节访问如下地址: 小书MybatisPlus第1篇-整合SpringBoot快速开始增删改查 小书MybatisPlus第2篇-条件构造器的应用及总结 小书MybatisPlus第3篇-自定义SQL 小书MybatisPlus第4篇-表格分页与下拉分页查询 Active Record 适合非常简单的领域需求,尤其在领域模型和数据库模型十分相似的情况下。如果遇到更加复杂的领域模型结构(例如用到继承、策略的领域模型),往往需要使用分离数据源的领域模型,结合 Data Mapper (数据映射器)使用。 具体到使用层面,我们之前章节讲过使用Data Mapper 做数据的持久层操作。 User user = new User(); user.setName("字母哥"); user.setAge(18); userMapper.insert(user); //Mybatis Mapper模式 现在我们使用ActiveRecord模式,用法如下,注意二者的区别 User user = new User(); user.setName("zimug"); user.setAge(18); user.insert(); //ActiveRecord模式 一、使ActiveRecord模式生效 首先:需要让数据库表对应的数据持久层实体类。集成自Model<User>

Specify custom index name when using add_reference

瘦欲@ 提交于 2020-07-04 20:53:29
问题 I have the following migration class LinkDoctorsAndSpecializations < ActiveRecord::Migration def up add_reference :doctors, :doctor_specialization, polymorphic: true, index: true end def down remove_reference :doctors, :doctor_specialization, polymorphic: true end end when i run rake db:migrate i am getting the error Index name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters so how can i specify the index

Specify custom index name when using add_reference

随声附和 提交于 2020-07-04 20:51:09
问题 I have the following migration class LinkDoctorsAndSpecializations < ActiveRecord::Migration def up add_reference :doctors, :doctor_specialization, polymorphic: true, index: true end def down remove_reference :doctors, :doctor_specialization, polymorphic: true end end when i run rake db:migrate i am getting the error Index name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters so how can i specify the index

How to define the sequence to use when creating a table in ActiveRecord migration in Ruby on Rails 5.2?

别来无恙 提交于 2020-06-26 06:26:48
问题 I need to assign a specific Postgres sequence to the ID field of my table. In the model, I tried to define the following setup which has no effect on Posgres: class MyObject < ActiveRecord::Base self.sequence_name = "global_seq" Usually, a table definition in ActiveRecord migrations start with create_table "objects", id: :serial, force: :cascade do |t| which generates a Postgres definition of column default value as default nextval('objects_id_seq'::regclass) How can I specify in the