我已经将rails_admin安装到我的应用程序中,我想要做一些非常基本的事情...我有两个模型,他们的关联按预期出现...我有一个研讨会注册模型belongs_to:user.
在rails_admin中,它将我的研讨会注册用户列为用户#1,用户#1等.
我想让它成为用户的名字.我设法做的是:
config.model SeminarRegistration do
label "Seminar Signups"
# Found associations:
configure :user, :belongs_to_association
configure :seminar_time, :belongs_to_association # # Found columns:
configure :id, :integer
configure :user_id, :integer # Hidden
configure :seminar_time_id, :integer # Hidden
configure :created_at, :datetime
configure :updated_at, :datetime # # Sections:
list do
field :user do
pretty_value do
user = User.find(bindings[:object].user_id.to_s)
user.first_name + " " + user.last_name
end
end
field :seminar_time
end
export do; end
show do; end
edit do; end
create do; end
update do; end …Run Code Online (Sandbox Code Playgroud) 我在寻找最优雅的方法来测试模型中属性的范围时遇到了一些麻烦。我的模型看起来像:
class Entry < ActiveRecord::Base
attr_accessible :hours
validates :hours, presence: true,
:numericality => { :greater_than => 0, :less_than => 24 }
end
Run Code Online (Sandbox Code Playgroud)
我的 rspec 测试如下所示:
require 'spec_helper'
describe Entry do
let(:entry) { FactoryGirl.create(:entry) }
subject { entry }
it { should respond_to(:hours) }
it { should validate_presence_of(:hours) }
it { should validate_numericality_of(:hours) }
it { should_not allow_value(-0.01).for(:hours) }
it { should_not allow_value(0).for(:hours) }
it { should_not allow_value(24).for(:hours) }
# is there a better way to test this range?
end
Run Code Online (Sandbox Code Playgroud)
这个测试有效,但是有没有更好的方法来测试最小值和最大值?我的方法看起来很笨拙。测试值的长度似乎很容易,但我不知道如何测试数字的值。我尝试过这样的事情: …