class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
irb中的:
> u = User.first
=> #<xxxxx>
> u.posts.class
=> Array
My env:
$ruby -v
ruby 1.9.2p312
$rails --version
--version
Rails 3.1.0
我记得几乎所有的关系查询都返回rails3中的ActiveRe
我最近才从Rails 2.3.5升级到Rails 3.2.7,并且在我的一些查询中注意到一些性能下降。我知道Rails 3 ActiveRecord在某些情况下比Rails 2.3.5慢,但是我的基准测试让我感到惊讶,我只是想确保我没有遗漏任何东西。
作为基准测试,我运行了以下查询,这在我的应用程序中非常流行
SELECT SQL_NO_CACHE table_name.* FROM table_name WHERE ((string_col = 'value') AND (int_col1 BETWEEN 5 AND 30)) ORDER BY int_col2 DESC L
我有一个项目资源和一个所有者资源。
rails g scaffold Item name:string
rails g scaffold Owner name:string
class Item < ActiveRecord::Base
has_one :owner
accepts_nested_attributes_for :owner
end
class Owner < ActiveRecord::Base
belongs_to :item
end
我的问题是我不能创建引用现有所有者对象的新Item对象。
In /db/migrate/create_owners
我对rails相当陌生。我有以下模型
class Question < ActiveRecord::Base
has_many :options
has_many :response_parts
end
class ResponsePart < ActiveRecord::Base
belongs_to :question
end
相应的脚手架是
rails g scaffold Question qorder:string qtext:text qtype:string
rails g scaffold ResponsePart answer:string que
我想在我的一个列上使用rank()函数PostgreSQL。
Character.select("*, rank() OVER (ORDER BY points DESC)")
但是,因为我的表中没有排名列,所以rails没有在查询中包含它。在我的ActiveRecord对象中包含排名的正确方法是什么?
在整个Rails中,有一个通过块变量查询参数的成语。
例如在迁徙中。
Rails中的典型迁移如下所示
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
t.integer :parent_id, foreign_key: true
end
end
end
重要的部分是传递给方法(如create_table )的块的变量。
我猜Rails使用了一些ruby元编程黑魔法来通过块变量|t|定
在Ruby on Rails项目中,我可以通过运行以下命令来获取查询结果
a = ActiveRecord::Base.connection.execute("select * from maluuba")
在rails控制台中,我可以看到对象类型是Mysql::Result。有人知道我在哪里可以找到这种类型的文档吗?
我使用Rails 3.2.8,并且有模型
class Subject < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :subjects
end
在SubjectsController中,索引
@subjects = Subject.includes(:users).all
在视图文件中,我希望通过以下方式显示主题的所有用户:
<%= subject.users.count if not sub
我是一名学习Ruby on Rails的PHP开发人员,我很喜欢ActiveRecord,并且我注意到一些非常有趣的事情,那就是ActiveRecord方法是如何检测方法链的末端来执行查询的。
@person = Person.where(name: 'Jason').where(age: 26)
# In my humble imagination I'd think that each where() executes a database query
# But in reality, it doesn't until the last method i
我有一个当前的城市和国家模型,就像这样
# City model
city:string
country_code:string
# Country model
country:string
country_code:string
我正在尝试使用country_code作为foreign_key来创建两个模型之间的关联,而不是使用默认的country_id。
# city.rb
belongs_to :country, :foreign_key => "country_code"
# country.rb
set_primary_key :country_code
h
"includes“和"joins”在ActiveRecord查询中有什么区别?谁能给我解释一下下面两个相关的模型?
class Car < ActiveRecord::Base
belongs_to :store
end
class Store < ActiveRecord::Base
belongs_to :owner
has_one :car
end
Problem:我正在寻找一个ActiveRecord (Rails 3.2)回调钩子,它将在DB事务开始之前执行。
如果它不存在(我不认为它存在),那么我正在寻找猴子补丁ActiveRecord的任何建议,以便我们可以自己实现这样的方法。
我试过什么
在查看了ActiveRecord文档和代码之后,似乎只有'after_commit‘才能被保证在事务之外执行。其他一切("before_validation“、"before_save”、"before_commit")都在事务中运行。我尝试过重写“transaction”方法:
class User &
所以我有3个模型:
# Rails table
class Library < ActiveRecord::Base
has_many :books
end
# Rails table
class Book < ActiveRecord::Base
belongs_to :library
end
# Global table used by lots of different apps in different languages
class Stat < ActiveRecord::Base
# No direct relationship because
使用Rails ActiveRecord构造where子句的最佳方式是什么?例如,假设我有一个返回博客帖子列表的控制器操作:
def index
@posts = Post.all
end
现在,假设我希望能够传递一个url参数,以便这个控制器操作只返回特定作者的帖子:
def index
author_id = params[:author_id]
if author_id.nil?
@posts = Post.all
else
@posts = Post.where("author = ?", author_id)
end
end
这对
我正在使用ActiveRecord进行rails项目。我正在进行一步一步的查询,而且由于我是Activerecord新手,我似乎错过了关键的概念。我无法在模型上获得id,因此无法执行查询。这是我的密码:
questions = JSON.parse(q)
selected_answers = questions.map do |ques|
ques['options'].map do |opt|
Answer.where(value: opt['title'])
end
end
participants = selected_answers.
如何验证最后一条记录没有在最后一分钟创建?
型号:
class Posts < ActiveRecord::Base
validates :author_id, :presence => true
belongs_to :author
before_save :check_timestamp
def check_timestamp
IF last_record.created_at(<=1 minute) THEN
DO NOT SAVE RECORD
end
end
我已经有一段时间没有拿
GraphQL查询是直接定义您是否搜索记录,例如通过其id或精确值(在我的例子中是date)。
我的查询应该返回的不是一个ActiveRecord对象,而是ActiveRecord关系。如何将其定义为可供GraphQL使用?
field :file_data, !FileDataType, " Returns records based on given date: format 'yyyy-mm-dd hh:mm:ss'" do
argument :created_on, !types.String, "format 'yyyy-mm-
我的问题与Rails相关,但即使是一个通用的SQL答案也会很有用。
我正在处理四个表:类别、书籍、categories_books和食谱。Category拥有并属于许多书籍。书中有很多食谱。
翻译成Rails的代码我有:
class Category < ActiveRecord::Base
has_and_belongs_to_many :books
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :categories
has_many :recipes
end
class Recipe &
使用Rails 4,并给出以下模型:
class Draft < ActiveRecord::Base
has_many :drafters
has_many :users, through: :drafters
end
class Drafter < ActiveRecord::Base
belongs_to :draft
belongs_to :user
end
class User < ActiveRecord::Base
has_many :drafters
has_many :drafts, through: :drafters
end
我的类如下所示,使用单表继承方法从用户继承客户。用户有属性名称和电子邮件,而订单有目的地。
class User < ActiveRecord::Base
end
class Customer < User
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
end
基于以下使用Rails控制台运行的代码片段
c = Customer.create
c.orders << Order.create
c.orders.delete_all
执行最后一行中的d