标签: ruby-on-rails-3

belongs_to通过协会

鉴于以下关联,我需要引用从模型中附加的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

我可能忽略了一些简单的事情.任何帮助,将不胜感激.

activerecord ruby-on-rails ruby-on-rails-3

133
推荐指数
3
解决办法
10万
查看次数

检查变量是否为整数

Rails 3或Ruby是否有内置方法来检查变量是否为整数?

例如,

1.is_an_int #=> true
"dadadad@asdasd.net".is_an_int #=> false?
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails-3

132
推荐指数
6
解决办法
17万
查看次数

使用Rake 0.9.0的未定义方法'任务'

我刚刚将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)

rake ruby-on-rails-3

132
推荐指数
5
解决办法
3万
查看次数

Rails 3:我想列出我的rails应用程序中定义的所有路径

我想列出我的rails 3应用程序中所有已定义的辅助路径功能(由路由创建),如果可能的话.

谢谢,

routes ruby-on-rails ruby-on-rails-3

132
推荐指数
6
解决办法
11万
查看次数

Emacs Ruby自动完成几乎正常工作

我一直在使用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)

有人可以解释为什么这会打破自动完成吗?

ide emacs elisp ruby-on-rails-3

132
推荐指数
1
解决办法
5190
查看次数

Rails:如何使用OR而不是AND链接范围查询?

我正在使用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

131
推荐指数
14
解决办法
10万
查看次数

Rails 3:获取随机记录

所以,我发现了几个在Rails 2中查找随机记录的例子 - 首选方法似乎是:

Thing.find :first, :offset => rand(Thing.count)
Run Code Online (Sandbox Code Playgroud)

作为一个新手,我不知道如何使用Rails 3中的新查找语法来构造它.

那么,找到随机记录的"Rails 3 Way"是什么?

ruby random activerecord ruby-on-rails ruby-on-rails-3

131
推荐指数
6
解决办法
7万
查看次数

如何清除卡住/陈旧的Resque工作人员?

从附图中可以看出,我有几个工人似乎被卡住了.这些过程不应超过几秒钟.

在此输入图像描述

我不确定为什么他们不会清除或如何手动删除它们.

我在Heroku上使用Resque with Redis-to-Go和HireFire来自动扩展工作人员.

ruby-on-rails heroku resque redis ruby-on-rails-3

131
推荐指数
6
解决办法
6万
查看次数

丢失了我的schema.rb!可以再生吗?

由于一些部署问题,我停止在git中跟踪schema.rb.不知何故,我已经填满了这个,并且我的schema.rb文件已经消失了.

有没有办法从数据库或迁移中重新生成schema.rb?我宁愿不丢失现有数据.

git ruby-on-rails ruby-on-rails-3

130
推荐指数
6
解决办法
5万
查看次数

Rails 3:"field-with-errors"包装器改变了页面外观.怎么避免这个?

电邮领域:

<label for="job_client_email">Email: </label> 
<input type="email" name="job[client_email]" id="job_client_email">
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

without_error

但是,如果电子邮件验证失败,它将变为:

<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)

看起来像这样:

with_error

我怎么能避免这种外观变化?

validation ruby-on-rails ruby-on-rails-3 field-with-errors

129
推荐指数
5
解决办法
5万
查看次数