你能告诉我创建has_one关系的最佳做法吗?
如果我有一个用户模型,它必须有一个配置文件...
我怎么能做到这一点?
一个解决方案是:
# user.rb
class User << ActiveRecord::Base
after_create :set_default_association
def set_default_association
self.create_profile
end
end
Run Code Online (Sandbox Code Playgroud)
但这似乎不是很干净......有什么建议吗?
ruby activerecord ruby-on-rails associations ruby-on-rails-3
我正在尝试启动并运行,但每当我启动服务器时,我都会看到"未初始化的常量ExceptionNotifier".
http://github.com/rails/exception_notification
在我的Gemfile中我有
gem"exception_notification",:git =>" http://github.com/rails/exception_notification.git " ,: branch =>"master"
我已经尝试将配置如config/application.rb,config/environment.rb和config.ru中的github readme所示.我用我的应用程序名称替换了"Whatever".
exception-handling ruby-on-rails ruby-on-rails-plugins ruby-on-rails-3
我想过使用观察者或回调.什么时候应该使用观察者?
你可以做以下事情:
# User-model
class User << AR
after_create :send_greeting!
def send_greeting!
UserNotifier.deliver_greeting_message(self)
end
end
#observer
class UserNotifier << AR
def greeting_message(user)
...
end
end
Run Code Online (Sandbox Code Playgroud)
或者您可以创建一个观察者并让它在用户创建时观察...
你推荐什么?
你能告诉我如何在rails路由中禁用.:format选项吗?我只需要HTML ...
你推荐用于标记的插件或宝石是什么?其中有很多,acts_as_taggable,acts_as_taggable_on_steroids,acts_as_taggable_on,...
你说什么?
你能告诉我在rails3中存储国际化常量的最佳实践吗?
我想为我的用户模型使用haircolours的常量哈希值:
# btw: how can I store such hashes in the locales.yml-files?
# en.yml
HAIR_COLOURS = { "brown" => 0, "white" => 1, "red" => 2, "dark-brown" => 3...}
# de.yml
HAIR_COLOURS = { "braun" => 0, "weiss" => 1, "rot" => 2, "dunkel-braun" => 3...}
# i18n.default_locale = :de
User.find(1).haircolour
=> 0
User.find(1).haircolour_str
=> "brown"
# i18n.default_locale = :de
User.find(1).haircolour
=> 0
User.find(1).haircolour_str
=> "braun"
Run Code Online (Sandbox Code Playgroud) hash ruby-on-rails constants internationalization ruby-on-rails-3
我有以下Rspec测试:
describe Productlimit do
before(:each) do
@productlimit = Factory.create(:productlimit, :user => Factory.create(:user))
end
subject { @productlimit }
...
it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
...
end
Run Code Online (Sandbox Code Playgroud)
但我得到以下令人困惑的错误:
1) Productlimit
Failure/Error: it { should validate_uniqueness_of(:price_cents).scoped_to(:direction_down, :currency, :market_id, :user_id) }
Expected errors to include "has already been taken" when price_cents is set to 9530, got errors: ["direction_down has already been taken (false)"]
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?我不明白为什么这不起作用,因为错误信息似乎是正确的?
编辑:
在其他情况下也会发生这种情况:
# product_spec.rb
...
it { should validate_numericality_of(:price).with_message("price_cents must be greater than 0 (0)") }
# …Run Code Online (Sandbox Code Playgroud) 我正在考虑一个社交网站.我的用户模型应该有一个属性"eyecolor",可以在带有选择框/下拉列表的视图页面上设置.
我的问题: - >我应该制作AR模型还是应该使用全局哈希/常量来获取数据?是否有"静态模型"的最佳实践?
如何在没有AR模型的情况下关联以下关系:
u = User.first
u.eyecolor
==> 1 (not the eyecolor-string!)
Run Code Online (Sandbox Code Playgroud)
我需要的是eyecolor-string:
u = User.first
u.eyecolor
==> "brown"
Run Code Online (Sandbox Code Playgroud)
谢谢,抱歉我的英语不好!
当我使用spork运行我的rspec测试时,每次使用capybara的save_and_open_page时,spork都会丢失测试套件..或者可能不再输出任何东西......
查看日志
# => without save_and_open_page
09:04:24 - INFO - Spork server for RSpec, Test::Unit successfully started
09:04:24 - INFO - Guard::RSpec is running
09:04:24 - INFO - Running all specs
Running tests with args ["--drb", "-f", "progress", "-r", "/Users/myuser/.rvm/gems/ruby-1.9.3-p392/gems/guard-rspec-2.5.2/lib/guard/rspec/formatter.rb", "-f", "Guard::RSpec::Formatter", "--failure-exit-code", "2", "spec"]...
................
Finished in 4.1 seconds
16 examples, 0 failures
Randomized with seed 50331
Done.
# => with save_and_open_page, no .... are shown anymore
09:04:29 - INFO - Guard is now watching at '/Users/myuser/coding/myproject'
09:04:39 - …Run Code Online (Sandbox Code Playgroud) 有没有办法从应用程序本身运行迁移?
谢谢!
ruby ×3
.net ×1
.net-2.0 ×1
.net-3.5 ×1
activerecord ×1
associations ×1
c# ×1
callback ×1
capybara ×1
constants ×1
formatting ×1
hash ×1
migrator.net ×1
routing ×1
rspec ×1
rubygems ×1
shoulda ×1
spork ×1
validation ×1