使用ActiveRecord模型,我知道您可以验证输入字段的长度,如下所示
class User
validates :user_name, length: { maximum: 20 }
end
然而,Rails中的一种设计模式推荐瘦模型。如果您有大量的验证,上面的代码可能看起来很吓人。我听说你还有别的办法可以做到这点。
您可以简单地使用ActiveRecord::Schema来完成相同的任务。
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
在使用自定义验证器运行我的rspec期望时,我得到了这个错误。
expect {@ua.save!}.to raise_error(ActiveRecord::RecordInvalid,'Validation failed: This question is no longer active')
失败,错误为
expected ActiveRecord::RecordInvalid with "Validation failed: This question is no longer active", got #<ActiveRecord::Recor
我需要通过在终端中使用命令生成来创建模型
rails generate model ...
如何使用该命令对列和primary_key字段名称之一设置唯一性验证?我发现如何才能做到这一点:
rails g model model_name field_name1:type:uniq field_name2:type:primary_key...
它为数据库primary_key中的表设置,并在迁移时对某些字段进行唯一性设置。但是,我想定义ActiveRecord模型的设置,这些设置将存储在我的项目的"models“文件夹中的"ModelName.rb”文件中。
现在,我每次操作
我正在从rails 2.3应用程序迁移到Rails 3.1,当我试图保存记录时,我会得到这个错误。这不是早些时候发生的,我不知道原因是什么,任何帮助都会受到感谢,谢谢。
以下是我的模型预览
**
class Store < ActiveRecord::Base
has_many :orders
end
**
**
class Order < ActiveRecord::Base
has_many :items, :dependent => :delete_all
end
**
**
class Item < ActiveRecord::Base
belongs_t
在Rails 4中,确保存在关联的最佳方法是什么?看起来您可以使用外键或关联变量本身进行测试。我对Rails还很陌生,所以这可能是个愚蠢的问题。我还认为,以下两种方法都可能有效。
# method 1:
class Region < ActiveRecord::Base
# In order to validate associated records whose presence is required,
# you must specify the :inverse_of option for the association:
has_many :flags, inv
使用Ruby关联,我有两个相互关联的模型
class Event < ActiveRecord::Base
belongs_to :post
和
class Post < ActiveRecord::Base
belongs_to :event
Post表有一个名为:anon的布尔列。在尝试创建事件时,我希望创建一个验证。如果anon字段为true,则关联事件有效,否则事件无效。我是否可以使用Rails关联来完成这个任务?
在Ruby on Rails中,我有一个名为Person的表。它有一个字段name。该表已经有很多行。他们中的一些人也有nil的名字。我可以处理已经在name字段中包含nil的记录,但是在下一步,我希望在有人创建新的Person条目时强制填写name字段。我正在使用活动记录验证来实现这一点:
在app/models/person.rb中
class Person < ActiveRecord::Base
validates :name, :presence => true
end
这会不会弄乱我的Person表,因为我已经有一些在name字段中为空的条目。
我在PostGres 9.5中使用Rails 4.2.7。我在Rails迁移中有这个专栏..。
my_num | integer |
在我的模型中,我希望筛选太大的数字,并将它们设置为零。例如,数字“659722222222222”正在我的模型上设置,并导致以下错误
Error during processing: (RangeError) 659722222222222 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Intege
我使用Devise在rails中进行用户身份验证。我的rails应用程序中有两个型号:-
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :userdetails
end
和
class Userdetail < ActiveRecord::Base
belongs_to :user
end
在user_deta
Job模型有一个整数job_price字段:
class CreateJobs < ActiveRecord::Migration
def self.up
create_table :jobs do |t|
...
t.integer "job_price"
...
end
end
...
end
如果用户在job_price字段中键入字符串,我希望显示一条错误消息,因此我添加了以下验证:
class Job < ActiveRecord::Base
validates_format_of :jo
我正试图在我的Ubuntu服务器上的一个新的rails项目上执行以下命令。
rails g scaffold User email:string username:string
但我收到以下错误:
"/usr/local/rvm/gems/ruby-1.9.3-p392/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Access denied for user 'root'@'localhost' (using password: YES) (Mysql2::Error)
我使用的是Rails 4.2.0和 gem。
此gem模拟ActiveRecord模型的多表继承。
我有我的父模型名为附件与子模型规范和发布。
class Attachment < ActiveRecord::Base
actable
end
class Specification < ActiveRecord::Base
acts_as :attachment
end
class Release < ActiveRecord::Base
acts_as :attachment
end
我的附件模型包含字段name、actable_id、actable_typ
我遇到了一种情况,即在闪存中保存的模型会导致marshall data too short错误,并发现原因是加载了一个巨大的关联,然后当将数据放入会话时,数据对于字段来说太大了。
我的课看起来是这样的:
class Location < ActiveRecord::Base
belongs_to :country
belongs_to :state
belongs_to :city
end
class Country < ActiveRecord::Base
has_many :states
has_many :locations
end
class Sta
由于升级到Rails 6,我无法启动after_rollback回调。我已经证实这在Rails、6.0.3.6或6.1.3.1上不起作用。任何帮助都是非常感谢的!
class User < ActiveRecord::Base
after_rollback do |user|
puts "You have rolled back!"
end
validates_presence_of :name
end
> User.first.update!(name: nil)
ActiveRecord::RecordInvalid (Validat