鉴于以下关联,我需要引用从模型中附加的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
我可能忽略了一些简单的事情.任何帮助,将不胜感激.
我正试图让我的头脑inverse_of,我不明白.
生成的sql是什么样的,如果有的话?
inverse_of如果与,和一起使用:has_many,该选项是否表现出相同的行为?:belongs_to:has_many_and_belongs_to
对不起,如果这是一个基本问题.
我看到了这个例子:
class Player < ActiveRecord::Base
has_many :cards, :inverse_of => :player
end
class Card < ActiveRecord::Base
belongs_to :player, :inverse_of => :cards
end
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"是什么?
我有一个关于Rails数据库的问题.
我应该在自动创建的"id"列中添加"index(unique)"吗?
如果我一次向两个外键添加索引(add_index (:users, [:category, :state_id])会发生什么?这与为每个键添加索引有何不同?
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.integer :category_id
t.integer :state_id
t.string :email
t.boolean :activated
t.timestamps
end
# Do I need this? Is it meaningless to add the index to the primary key?
# If so, do I need :unique => true ?
add_index :users, :id
# I don't think I need ":unique => true here", right?
add_index :users, :category_id # Should I need this? …Run Code Online (Sandbox Code Playgroud)我一直无法在Rails中找到关于.build方法的任何文档(我目前正在使用2.0.2).
通过实验,似乎您可以使用构建方法has_many在保存记录之前将记录添加到关系中.
例如:
class Dog < ActiveRecord::Base
has_many :tags
belongs_to :person
end
class Person < ActiveRecord::Base
has_many :dogs
end
# rails c
d = Dog.new
d.tags.build(:number => "123456")
d.save # => true
Run Code Online (Sandbox Code Playgroud)
这将正确地保存带有外键的狗和标签.这似乎不适用于某种belongs_to关系.
d = Dog.new
d.person.build # => nil object on nil.build
Run Code Online (Sandbox Code Playgroud)
我也试过了
d = Dog.new
d.person = Person.new
d.save # => true
Run Code Online (Sandbox Code Playgroud)
Dog在这种情况下未设置外键,因为在保存时,新人没有id,因为它尚未保存.
我的问题是:
如何构建工作,以便Rails足够聪明,弄清楚如何以正确的顺序保存记录?
我怎样才能在belongs_to关系中做同样的事情?
我在哪里可以找到有关此方法的任何文档?
谢谢
当我有一些id,比如
ids = [2,3,5]
Run Code Online (Sandbox Code Playgroud)
我表演
Comment.find(ids)
Run Code Online (Sandbox Code Playgroud)
一切正常.但是当存在不存在的id时,我会得到一个例外.当我获得与某些过滤器匹配的ID列表时,通常会发生这种情况
current_user.comments.find(ids)
Run Code Online (Sandbox Code Playgroud)
这次我可能有一个有效的评论ID,但不属于给定的用户,所以找不到它,我得到一个例外.
我试过了find(:all, ids),但它返回了所有记录.
我现在能做到的唯一方法是
current_user.comments.select { |c| ids.include?(c.id) }
Run Code Online (Sandbox Code Playgroud)
但在我看来,这似乎是超低效的解决方案.
有没有更好的方法在Array中选择ID而不会在不存在的记录上获得异常?
我正在尝试手动执行SQL命令,以便我可以访问NuoDB中的过程.
我正在使用Ruby on Rails,我正在使用以下命令:
ActiveRecord::Base.connection.execute("SQL query")
Run Code Online (Sandbox Code Playgroud)
"SQL查询"可以是任何SQL命令.
例如,我有一个名为"反馈"的表,当我执行命令时:
ActiveRecord::Base.connection.execute("SELECT `feedbacks`.* FROM `feedbacks`")
Run Code Online (Sandbox Code Playgroud)
这只会返回"真实"响应,而不是向我发送所有请求的数据.
这是Rails控制台上的输出是:
SQL (0.4ms) SELECT `feedbacks`.* FROM `feedbacks`
=> true
Run Code Online (Sandbox Code Playgroud)
我想用它来调用NuoDB中的存储过程,但是在调用过程时,这也会返回一个"true"响应.
无论如何我是否可以执行SQL命令并获取请求的数据而不是获得"真实"的响应?
我在Rails 3中搜索了这个:
Note.where(:user_id => current_user.id, :notetype => p[:note_type], :date => p[:date]).order('date ASC, created_at ASC')
Run Code Online (Sandbox Code Playgroud)
但我需要:date => p[:date]条件是等同的:date > p[:date].我怎样才能做到这一点?谢谢阅读.