鉴于以下关联,我需要引用从模型中附加的Questiona .我一直在尝试使用它来执行此操作.ChoiceChoicebelongs_to :question, through: :answer
class User
has_many :questions
has_many :choices
end
class Question
belongs_to :user
has_many :answers
has_one :choice, :through => :answer
end
class Answer
belongs_to :question
end
class Choice
belongs_to :user
belongs_to :answer
belongs_to :question, :through => :answer
validates_uniqueness_of :answer_id, :scope => [ :question_id, :user_id ]
end
Run Code Online (Sandbox Code Playgroud)
我正进入(状态
NameError未初始化的常量
User::Choice
当我尝试做的时候 current_user.choices
它工作正常,如果我不包括
belongs_to :question, :through => :answer
Run Code Online (Sandbox Code Playgroud)
但我想用它,因为我希望能够做到这一点 validates_uniqueness_of
我可能忽略了一些简单的事情.任何帮助,将不胜感激.
Rails 3或Ruby是否有内置方法来检查变量是否为整数?
例如,
1.is_an_int #=> true
"dadadad@asdasd.net".is_an_int #=> false?
Run Code Online (Sandbox Code Playgroud) 我刚刚将Rake更新到最新版本(0.9.0.beta.4),该rake命令最终出现以下错误消息:
rake aborted!
undefined method `task' for #<Anelis::Application:0x9223b6c>
Run Code Online (Sandbox Code Playgroud)
这是跟踪:
undefined method `task' for #<Anelis::Application:0x97ef80c>
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:214:in `initialize_tasks'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:139:in `load_tasks'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
/home/amokrane/Documents/prog/web/learning_rails/anelis/Rakefile:7:in `load_string'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/environment.rb:28:in `eval'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/environment.rb:28:in `load_string'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/environment.rb:16:in `load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:495:in `raw_load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:78:in `block in load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:77:in `load_rakefile'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:61:in `block in run'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/lib/rake/application.rb:59:in `run'
/usr/local/rvm/gems/ruby-1.9.2-p136/gems/rake-0.9.0.beta.4/bin/rake:31:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.2-p136/bin/rake:19:in `load'
/usr/local/rvm/gems/ruby-1.9.2-p136/bin/rake:19:in `<main>'
Run Code Online (Sandbox Code Playgroud)
有人经历过同样的问题吗?什么可能是错的?请注意,我正在运行Rails 3.0.3,您可能也对我的Gemfile的内容感兴趣:
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'mysql2'
gem 'legacy_data'
gem 'resources_controller', :git …Run Code Online (Sandbox Code Playgroud) 我想列出我的rails 3应用程序中所有已定义的辅助路径功能(由路由创建),如果可能的话.
谢谢,
我一直在使用Rsense更新我的emacs配置,以允许在键入代码时出现自动完成下拉框.这在大多数文件中运行良好,除了我发现当我在ruby on rails项目中编辑一些代码时,它不允许我从表中选择答案.
这是我的设置:https: //github.com/map7/simple_emacs
我在Ubuntu 10.04下使用它.
对于简单的ruby脚本文件,它工作得很好.我可以打开一个新文件并输入.
"test".up...
Run Code Online (Sandbox Code Playgroud)
就像我输入'p'字符一样,出现了一个选项列表,我可以用箭头键在列表中上下移动,然后用回车键选择一个(例如:upcase).
什么不起作用是当我做一个完全相同的测试,但在rails项目的基本目录中.
更新:
发现问题在于(需要'rails),所以在emacs-rails插件中,自动完成不喜欢它.
更新:
它位于emacs-rails - > rails-project.el中.如果我评论这个宏,那么自动完成工作,否则它不会:
(defmacro* rails-project:with-root ((root) &body body)
"If you use `rails-project:root' or functions related on it
several times in a block of code, you can optimize your code by
using this macro. Also, blocks of code will be executed only if
rails-root exist.
(rails-project:with-root (root)
(foo root)
(bar (rails-core:file \"some/path\")))
"
`(let ((,root (rails-project:root)))
(when ,root
(flet ((rails-project:root () ,root))
,@body))))
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这会打破自动完成吗?
我正在使用Rails3,ActiveRecord
只是想知道如何用OR语句而不是AND来链接范围.
例如
Person.where(:name => "John").where(:lastname => "Smith")
Run Code Online (Sandbox Code Playgroud)
这通常会返回name ='John'和lastname ='Smith',但我想:
name ='John'或lastname ='Smith'
activerecord ruby-on-rails ruby-on-rails-3 rails-activerecord
所以,我发现了几个在Rails 2中查找随机记录的例子 - 首选方法似乎是:
Thing.find :first, :offset => rand(Thing.count)
Run Code Online (Sandbox Code Playgroud)
作为一个新手,我不知道如何使用Rails 3中的新查找语法来构造它.
那么,找到随机记录的"Rails 3 Way"是什么?
从附图中可以看出,我有几个工人似乎被卡住了.这些过程不应超过几秒钟.

我不确定为什么他们不会清除或如何手动删除它们.
我在Heroku上使用Resque with Redis-to-Go和HireFire来自动扩展工作人员.
由于一些部署问题,我停止在git中跟踪schema.rb.不知何故,我已经填满了这个,并且我的schema.rb文件已经消失了.
有没有办法从数据库或迁移中重新生成schema.rb?我宁愿不丢失现有数据.
电邮领域:
<label for="job_client_email">Email: </label>
<input type="email" name="job[client_email]" id="job_client_email">
Run Code Online (Sandbox Code Playgroud)
看起来像这样:

但是,如果电子邮件验证失败,它将变为:
<div class="field_with_errors">
<label for="job_client_email">Email: </label>
</div>
<div class="field_with_errors">
<input type="email" value="wrong email" name="job[client_email]" id="job_client_email">
</div>
Run Code Online (Sandbox Code Playgroud)
看起来像这样:

我怎么能避免这种外观变化?
ruby-on-rails-3 ×10
activerecord ×3
ruby ×2
elisp ×1
emacs ×1
git ×1
heroku ×1
ide ×1
rake ×1
random ×1
redis ×1
resque ×1
routes ×1
validation ×1