我有一个复杂的ActiveRecord查询,我正在构建不同的范围,具体取决于用户选择.我正在使用2个宝石,这似乎有问题,但我无法找到谁是罪魁祸首:Texticle(对于Postgresql全搜索)(2.0.3)Squeel(用于Active Record查询中的ruby语法)squeel(0.9. 5)Arel或Active Record本身
这是我的类定义:
class Event < ActiveRecord::Base
belongs_to :entity, :class_name => "Entity", :foreign_key => :entity_id
belongs_to :place, :class_name => "Entity", :foreign_key => :place_id
class Entity < ActiveRecord::Base
has_many :events, :foreign_key => 'entity_id'
has_many :events, :foreign_key => 'place_id'
Run Code Online (Sandbox Code Playgroud)
最后,我的查询:
Event.joins{entity.outer}.joins{place.outer}.includes(:place).includes(:entity).textsearch('anystring'.downcase)
Run Code Online (Sandbox Code Playgroud)
*这是一个ActiveRecord :: Relation对象,在使用to_a调用它时会崩溃
这给了我以下错误:
ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table "entities_events"
LINE 1: ... AS t1_r30, "entities"."metro_area_id" AS t1_r31, "entities_...
^
: SELECT "events"."id" AS t0_r0, "events"."name" AS t0_r1, "events"."start_at" AS t0_r2, "events"."end_at" AS t0_r3, "events"."created_at" AS …Run Code Online (Sandbox Code Playgroud) 我想使用Squeel重构下面的查询.我想这样做,以便我可以链接其中的运算符并重新使用查询的不同部分中的逻辑.
User.find_by_sql("SELECT
users.*,
users.computed_metric,
users.age_in_seconds,
( users.computed_metric / age_in_seconds) as compound_computed_metric
from
(
select
users.*,
(users.id *2 ) as computed_metric,
(extract(epoch from now()) - extract(epoch from users.created_at) ) as age_in_seconds
from users
) as users")
Run Code Online (Sandbox Code Playgroud)
查询必须全部在DB中运行,并且不应该是混合Ruby解决方案,因为它必须对数百万条记录进行排序和切片.
我已经设置了问题,因此它应该针对普通user表运行,以便您可以使用它的替代方案.
User具有所有常规属性的对象extra_metric_we_care_about,age_in_seconds和compound_computed_metric下面的解决方案不起作用,但它显示了我希望实现的优雅类型
class User < ActiveRecord::Base
# this doesn't work - it just illustrates what I want to achieve
def self.w_all_additional_metrics
select{ ['*',
computed_metric,
age_in_seconds,
(computed_metric …Run Code Online (Sandbox Code Playgroud) 有没有办法在Squeel中为连接添加"on"条件.如果没有,还有其他替代方案可以实现相同的目标吗?
如果要覆盖Rails默认连接映射,可以使用Arel on()来完成.在Squeel有同等的电话来完成同样的事情吗?
谢谢.
scope :for_user, (lambda {|user_id| a = Follow.follows(user_id); Question.where{user_id.in(a.select{followed_id})}})
Run Code Online (Sandbox Code Playgroud)
给我:
`lambda': tried to create Proc object without a block (ArgumentError)
Run Code Online (Sandbox Code Playgroud)
我已经阅读了几个问题但无法解决问题。我对 Ruby 比较陌生,刚刚开始使用 Rails。我可能有点有点不知所措了。
说我有一套条件:
Person.where{(name =~ 'Ernie%') & (salary < 50000) | (name =~ 'Joe%') & (salary > 100000)}
Run Code Online (Sandbox Code Playgroud)
...将生成如下SQL:
SELECT "people".* FROM people
WHERE ("people"."name" LIKE 'Ernie%' AND "people"."salary" < 50000)
OR ("people"."name" LIKE 'Joe%' AND "people"."salary" > 100000)
Run Code Online (Sandbox Code Playgroud)
如何反转返回的集合,即更新Squeel以返回以下sql:
SELECT "people".* FROM people
WHERE NOT(("people"."name" LIKE 'Ernie%' AND "people"."salary" < 50000)
OR ("people"."name" LIKE 'Joe%' AND "people"."salary" > 100000))
Run Code Online (Sandbox Code Playgroud)
这有点人为,因为我的问题还涉及一组动态生成的条件 - 我无法改变实际条件,只需要将它们包装在NOT()中.
任何关于在哪里寻找的想法或建议?
我试图找到一种方法来创建一个简单的外连接,而不会有太多的麻烦.我知道我可以通过指定外连接手动完成此操作,但我正在寻找一种简单的方法.
因此,我看看Squeel,这似乎是Metawhere的新选择.它似乎能够处理外连接,但我无法得到我想要的东西.
特别是,我有三个模型:
City
Building
CityBuilding
Run Code Online (Sandbox Code Playgroud)
我只想要列出所有建筑物,无论它们是否存在于城市中.当然,城市建筑是将城市与建筑连接起来的模型.我想得到类似的东西:
city 1{
TownCenter => city_building
Sawmill => city_building
Quarry => nil
}
Run Code Online (Sandbox Code Playgroud)
查询为null,因为没有city_building这个条目,你明白了.
Squeel有没有办法做到这一点?或者可能是另一个宝石,而不必手动进行外部连接?
我正在将一个大型应用程序拆分成多个较小的应用程序.在这样做的时候,我意识到通过Gemfile.global在我的主应用程序和子应用程序中创建并包含它,我可以清理我Gemfile的所有内容.例如,我的所有部署宝石都在主要部分Gemfile,而我的导轨进入了Gemfile.global.
它几乎适用于我的所有宝石,除了一个:Squeel.
我Gemfile在根应用程序中开始:
gemfiles = [
File.join('Gemfile.global'),
]
Dir.glob(File.join(File.dirname(__FILE__), gemfiles)) do |gemfile|
eval(IO.read(gemfile), binding)
end
# gem 'squeel' # Let's try putting this in Gemfile.global
Run Code Online (Sandbox Code Playgroud)
我Gemfile.global看起来像:
source 'https://rubygems.org'
# rails and dependencies
gem 'squeel'
Run Code Online (Sandbox Code Playgroud)
bundle installrails sSqueel在主要方面表现出色,以及几乎所有东西,你期望的方式Gemfile.Gemfile.global但是,在使用默认的Squeel初始值设定项时,将其置于初始化过程中:
Squeel.configure do |config|
end
Run Code Online (Sandbox Code Playgroud)
rails suninitialized constant Squeel (NameError)即使bundle install报道,也会抛出一个Using squeel (1.0.13).为什么这个组合我Gemfile的陷阱运行时常量的方法?
假设我有一个模型:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
end
Run Code Online (Sandbox Code Playgroud)
我想定义一个scope名为的查询completed:
返回以下所有问题:
- 标题不是空的OR
- 至少有1张照片
我怎样才能做到这一点?
到目前为止,我有:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
scope :completed, where{title != ""} # returns all questions with non-empty title
end
Run Code Online (Sandbox Code Playgroud)
如果我能说:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
scope :completed, where{title != "" || pictures.count > 0}
end
Run Code Online (Sandbox Code Playgroud) 我不知道为什么这两段代码在Ruby 1.8.7中表现不同,因为一个似乎是另一个的单行版本.
第一段代码(它应该工作):
if @type.present?
type = @type
orders = Order.where{type.eq(type)}
end
Run Code Online (Sandbox Code Playgroud)
单行版本(根本不起作用,没有错误但似乎没有执行):
orders = Order.where{type.eq(type)} if (type = @type).present?
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用squeel gem,这就是块跟随where方法的原因.此外,变量类型必须捕获实例变量@type,因为执行上下文在块内部发生更改,并且实例变量不在主上下文和块上下文之间共享.
注2:出于遗留原因,我必须使用Ruby 1.8.7.
任何的想法?谢谢!
我有这个Ruby代码:
def visits_chart_data(domain)
last_12_months.collect do |month|
{ created_at: month, count: domain.visits.created_on_month(month).count }
end
end
def last_12_months
(0..11).to_a.reverse.collect{ |month_offset| month_offset.months.ago.beginning_of_month }
end
Run Code Online (Sandbox Code Playgroud)
CreatedOnMonth只是一个范围:
scope :created_on_month, -> (month = Time.now) { where{ (created_at >= month.beginning_of_month) & (created_at <= month.end_of_month) } }
Run Code Online (Sandbox Code Playgroud)
created_at 只是一个标准的日期时间戳.
如何优化它以进行一次查询而不是12?
我看到有些人使用GROUP_BY,但是我对PostgreSQL不太好,能够自己构建这样的查询.查询应按年份对记录进行分组并返回计数.也许有人可以帮助我.谢谢.
假设我有这个数据(注意author_id第三本书中的空值):
Authors
-------------------------
id | name
-------------------------
1 | Susan Collins
2 | Steven Meyer
-------------------------
Books
--------------------------------------
id | author_id | title
--------------------------------------
1 | 1 | Hunger Games
2 | 2 | Twilight
3 | nil | Games of Throne
--------------------------------------
Run Code Online (Sandbox Code Playgroud)
我有一个书籍搜索栏。我希望它可以通过书名和作者姓名进行搜索。
例如,当我搜索“苏珊”时。它将返回标题为“Susan”或作者名称为“Susan”之类的书籍。
问题是:当我搜索“游戏”时,它只返回“饥饿游戏”。它没有选择“权力的游戏”,因为它author_id是空的。
我在这里制作了SQLFiddle。
这是从我的 Rails 代码生成的 SQL:
SELECT books.* FROM books
INNER JOIN authors ON authors.id = books.author_id
WHERE (
( LOWER(books.title) LIKE LOWER("%game%") )
OR
( LOWER(authors.name) LIKE …Run Code Online (Sandbox Code Playgroud) squeel ×11
ruby ×4
activerecord ×3
arel ×3
gem ×1
gemfile ×1
mysql ×1
postgresql ×1
rubygems ×1
sql ×1