在本文中,我们将详细介绍RailsActiverecord关系:使用子查询作为SQLselect语句的表的各个方面,同时,我们也将为您带来关于activerecord–Rails3:使用form_fo
在本文中,我们将详细介绍Rails Activerecord关系:使用子查询作为SQL select语句的表的各个方面,同时,我们也将为您带来关于activerecord – Rails 3:使用form_for的非Active Record对象、activerecord – ruby-on-rails3,并使用activereccord3选择distinct、mysql – 防止Rails缓存ActiveRecord查询的结果、Rails 5:使用构建时ActiveRecord是否使用ID序列的有用知识。
本文目录一览:- Rails Activerecord关系:使用子查询作为SQL select语句的表
- activerecord – Rails 3:使用form_for的非Active Record对象
- activerecord – ruby-on-rails3,并使用activereccord3选择distinct
- mysql – 防止Rails缓存ActiveRecord查询的结果
- Rails 5:使用构建时ActiveRecord是否使用ID序列
Rails Activerecord关系:使用子查询作为SQL select语句的表
有人可以帮我弄清楚如何使用Rails(我正在使用Rails
4)Activerecord方法编写以下SQL吗?我知道您可以使用find_by_sql执行此操作,但我想保留活动记录关系。这是我要创建的postGreSQL数据库的sql:
SELECT * FROM
(SELECT DISTINCT ON(table_a.id) table_a.name as alias_a,table_b.id,table_b.time
FROM table_1
LEFT OUTER JOIN table_b ON table_a.id = table_b.id
ORDER BY table_a.id,table_b.time asc) AS subquery
ORDER BY alias_a asc
对于我的子查询,我具有以下内容(它将生成上面的子查询的sql):
@subquery = table_a.select("DISTINCT ON(table_a.id) table_a.name as alias_a,table_b.time")
@subquery = @subquery.joins("LEFT OUTER JOIN table_b ON table_a.id = table_b.id")
@subquery = @subquery.order("table_a.id,table_b.time asc")
但是,我不知道如何编写使用@subquery作为外部select语句表的select语句。
activerecord – Rails 3:使用form_for的非Active Record对象
解决方法
看看here.
看看Github的源代码.
activerecord – ruby-on-rails3,并使用activereccord3选择distinct
特别是下面的电话!
Error.find(:all,:select => 'disTINCT type')
有人有一个想法,如何将此调用转换为ActiveRecord3有效语句?
我在网路上找不到任何东西
谢谢
解决方法
select
查询方法.
Error.select('disTINCT type')
mysql – 防止Rails缓存ActiveRecord查询的结果
我有一个rake任务,需要遍历大量的记录(称为商家),每个记录都有大量的关联项.我的问题是,由于Rails自动缓存我的数据库查询的结果,我最终将我的工作人员放入交换空间很长时间.
简而言之,我想知道如何运行如下命令:
Merchant.all.each {| m | items = m.items}
没有缓存每次“物品”的价值.
我试过了:
Merchant.all.each do |m|`
ActiveRecord::Base.connection.uncached do
items = m.items
end
end
我也尝试将其添加到我的Merchant模型中:
def items_uncached
self.class.uncached { items }
end
然后调用items_uncached,但我仍然最终使用我访问的每组新项目来增加内存使用量.
我正在运行Rails 2.3.10,Ruby 1.9.2并使用MysqL进行存储.
提前感谢您的想法!
*编辑:
正是我正在研究的实际代码:
File.open(output, "w") do |f|
Merchant.all.each do |m|
items = m.items
invalid_image_count = 0
items.each do |i|
invalid_image_count += 1 unless i.image_valid?
end
invalid_categories = items.select { |i| !i.categories_valid? }.count
f.puts "#{m.name} (#{m.id}): #{invalid_image_count} invalid images, " +
"#{invalid_categories} invalid categories"
end
end
尝试进行一些错误检查,然后记录结果.
解决方法:
如果您的关联是一个简单的has_many关联,您可以尝试这样做:
Merchant.all.each do |m|
items = Item.find_all_by_merchant_id(m.id)
...
end
甚至:
Merchant.find(:all, :select => "id, name").each do |m|
items = Item.find_all_by_merchant_id(m.id)
...
end
Rails 5:使用构建时ActiveRecord是否使用ID序列
如何解决Rails 5:使用构建时ActiveRecord是否使用ID序列?
我有2个具有1对1关系的模型,即Project
和inspection
,其结构类似于
class Project < ApplicationRecord
has_one :inspection
def inspection_required?
(inspection || build_inspection).required?
end
end
class inspection < ApplicationRecord
belongs_to :project
def required?
required_saved || API.check({ id: id})
end
end
如上所示,对于Project
,如果我们没有inspection
,则它build_inspection
。我查看了数据库,似乎inspection
记录id
跳过了如下所示的顺序,
ID | PROJECT_ID | vendOR_TYPE | required_SAVED | CREATED_AT | UPDATED_AT |
---|--------------|-------------|----------------|---------------------|----------------------|
1 | 7983f1a33d55 | vendor1 | false | 2020-09-12 00:43:52 | 2020-09-12 00:43:52 |
3 | asdereww224f | vendor2 | true | 2020-09-12 00:48:51 | 2020-09-12 00:48:51 |
4 | 3q3332reere1 | vendor1 | false | 2020-09-13 01:02:53 | 2020-09-13 01:02:53 |
7 | 1df343qe233a | vendor2 | true | 2020-09-14 02:43:51 | 2020-09-14 02:43:51 |
ID列正在跳过序列。
所以我的问题是,即使我们不保存记录, build_inpsection
是否也在使用Postgres sql生成的序列?
Rails版本:Rails 5.2.3
Postgres版本:9.6
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
我们今天的关于Rails Activerecord关系:使用子查询作为SQL select语句的表的分享就到这里,谢谢您的阅读,如果想了解更多关于activerecord – Rails 3:使用form_for的非Active Record对象、activerecord – ruby-on-rails3,并使用activereccord3选择distinct、mysql – 防止Rails缓存ActiveRecord查询的结果、Rails 5:使用构建时ActiveRecord是否使用ID序列的相关信息,可以在本站进行搜索。
本文标签: