Tag: sql

如何通过关联表检索一组用户共有的关联对象/记录?

我正在使用Ruby on Rails 3.2.2,我想检索由两个或更多用户同时关联的对象/记录。 也就是说,我有一个数据库表,我在其中存储用户和文章之间的关联数据; 我想“构建”一个SQL查询,以便由两个或更多用户检索相关文章。 例如,如果我有以下关联对象 # # # # # # # # 我想陈述/运行一个范围方法,以便获得如下内容: @user1.articles.associated_by(@user2) # => [ #, #] @user1.articles.associated_by(@user3) # => [ #, #] @user1.articles.associated_by(@user4) # => nil @user2.articles.associated_by(@user3) # => [ #] @user1.articles.associated_by([@user2, @user3]) # => [ #] 换句话说, 我想通过user_article_associations表找到一组用户共同的user_article_associations 。 我该怎么做? 涉及的类别表示为 class User ‘UserArticleAssociation’ has_many :articles, :through => […]

Ruby on Rails在哪里查询关系

我试图使用查询与关系。 在这种情况下,如何使用where关系查询? 这是模特 User has_many :projects has_many :reasons, through: :projects Project belongs_to :user has_many :reasons Reasons belongs_to :project 这是不起作用的代码 # GET /reasons def index reasons = current_user.reasons updated_at = params[:updated_at] # Filter with updated_at for reloading from mobile app if updated_at.present? # This one doesn’t work!!!!!!!!!!!! reasons = reasons.includes(:projects).where(“updated_at > ?”, Time.at(updated_at.to_i)) # Get all […]

Rails:如何选择没有特定相关(关联)对象的记录(SQL EXISTS简要操作方法)

假设我们有用户: class User < ActiveRecord::Base has_many :connections has_many :groups, through: :connections end 和团体: class Group < ActiveRecord::Base has_many :connections has_many :users, through: :connections end 基本上,标准的多对多连接: class Connection belongs_to :user belongs_to :group end 我打算做的是: 仅选择不属于给定的一组组的用户(具有ID的组[4,5,6] ) 仅选择属于一组( [1,2,3] )且不属于另一组( [4,5,6] )的用户 仅选择不属于组的用户 另外,我不想: 从数据库中获取大量数据以使用Ruby代码对其进行操作。 我知道在CPU和内存方面效率低下(Ruby比任何常用的数据库引擎慢得多,通常我想依靠数据库引擎来完成繁重的工作) 我尝试了像User.joins(:group).where(group_id: [1,2,3]).where.not(group_id: [4,5,6]) ,它们返回错误的结果(结果中的一些用户)集合属于组4,5,6 以及 1,2,3) 我不想仅仅为了检查存在而进行连接,因为我知道对于DB来说这是一个非常复杂的(即CPU /内存密集型)操作

使用带有连接的占位符

我试图通过在连接上替换我的参数来避免任何SQL注入漏洞。 Category.joins(“LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = ?”, params[:Dept]) 这会尝试在其中执行带有问号的查询,而不是将其替换为param。 这样做的正确方法是什么? 编辑: 查询需要返回: SELECT categories.* FROM “categories” LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86 不 SELECT categories.* FROM “categories” LEFT OUTER JOIN incomes ON incomes.category_id = categories.id WHERE incomes.dept_id = 86 结果非常不同!

Arel在聚合上导致无限循环

我在使用Arel在同一查询中聚合2列时遇到问题。 当我运行它时,整个服务器冻结了一分钟,在rails dev-server崩溃之前。 我怀疑是无限循环:)。 也许我误解了Arel的整个概念,如果有人能看一下,我将不胜感激。 此查询的预期结果如下所示:[{:user_id => 1,:sum_account_charges => 300,:sum_paid_debts => 1000},…] a_account_charges = Table(:account_charges) a_paid_debts = Table(:paid_debts) a_participants = Table(:expense_accounts_users) account_charge_sum = a_account_charges .where(a_account_charges[:expense_account_id].eq(id)) .group(a_account_charges[:user_id]) .project(a_account_charges[:user_id], a_account_charges[:cost].sum) paid_debts_sum = a_paid_debts .where(a_paid_debts[:expense_account_id].eq(id)) .group(a_paid_debts[:from_user_id]) .project(a_paid_debts[:from_user_id], a_paid_debts[:cost].sum) charges = a_participants .where(a_participants[:expense_account_id].eq(id)) .join(account_charge_sum) .on(a_participants[:user_id].eq(account_charge_sum[:user_id])) .join(paid_debts_sum) .on(a_participants[:user_id].eq(paid_debts_sum[:from_user_id]))

Ruby ActiveRecord和sql元组支持

ActiveRecord是否支持where子句中的元组,假设底层数据库有? 生成的where子句看起来像: where (name, address) in ((‘John’, ‘123 Main St’)) 我试过了: Person.where({[:name, :address] => [‘John’, ‘123 Main St’]}) 它不起作用。

Rails + PostgreSQL SSL解密失败

我在我的生产服务器上运行了一个应用程序,它使用pg gem与Postgres数据库进行通信。 Postgres在默认端口上运行,并且位于防火墙之后 – 因此除了localhost ,它无法访问。 我没有配置Postgres做任何与SSL相关的事情。 我正在通过SSL访问Rails应用程序,并且证书已经签署了另一个域,所以第一次点击它时会出现证书错误…但这是SSL相关的唯一奇怪的东西。 然而,我在Rails日志中间歇性地看到这一点(当浏览器发生时伴随着500错误): Started GET “/admin/pages” for at 2012-02-02 01:52:03 -0500 Processing by PagesController#index as HTML Completed 500 Internal Server Error in 4ms ActiveRecord::StatementInvalid (PGError: SSL error: decryption failed or bad record mac : SELECT “pages”.* FROM “pages” ): app/controllers/pages_controller.rb:36:in `index’ 我勒个去?

在rails模型中编写大型SQL的更好方法是什么?

在使用Rails为糖代码提供的很多Arel之后,我在处理大型复杂的SQL查询时遇到了问题,而我无法用Arel方法做得很好。 我喜欢Arel的小东西,但是当它变得混乱时,我更喜欢分开代码。 那么,有什么建议我应该如何处理模型中的大型SQL ? 就像,我什么时候应该为此创建SQL视图 (因为我看到Rails不能很好地提供,我必须为此创建一个迁移)或在某个文件夹“sqls”中创建任何单独的类,然后从那里调用。 我知道有些人使用<< – SQL表达式 这是我目前的例子: Question.from(self.questions .select(“questions.id”) .select(“(NOT (questions.last_active_user_id = #{user.id} OR (COALESCE(ss.updated_at > questions.last_active_at, false) OR COALESCE(ds.updated_at > questions.last_active_at, false))))::integer as active”) .select(“(((NOT((COALESCE(ss.updated_at > questions.created_at, false) OR COALESCE(ds.updated_at > questions.created_at, false))) AND pages.owner_id = questions.user_id) OR (NOT (COALESCE(ss.updated_at > questions.owner_found_important_at, false) OR COALESCE(ds.updated_at > questions.owner_found_important_at, false)) AND owner_found_important_at is […]

ActiveRecord的:: StatementInvalid。 PG错误

我试图使用Project.find(id)从Project模型中找到一个项目,但它给了我ActiveRecord::StatementInvalid错误 完全追踪 – PG::Error: ERROR: prepared statement “a1” already exists : SELECT COUNT(*) FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind in (‘v’,’r’) AND c.relname = $1 AND n.nspname = ANY (current_schemas(false)) /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1180:in `prepare’ /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1180:in `prepare_statement’ /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1144:in `exec_cache’ /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord- 3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `block in exec_query’ /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log’ /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activesupport-3.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument’ /home/deploy/.rvm/gems/ruby-1.9.2-p290@submit_contactpl/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:275:in […]

Rails ActiveRecord按连接表关联的数量排序

我有一个Resource模型,可以使用“Acts As Votable”gem( Github页面 )进行投票。 投票系统运作完美,但我试图按每个Resource有多少votes来显示页面。 目前我的控制器根据标签提取资源,而不是订购: @resources = Resource.where(language_id: “ruby”) 如果我获取一个单独的资源并调用“@ resource.votes.size”,它将返回它有多少票。 但是,投票是另一个表,所以我认为需要进行某种联接,但我不知道如何做。 我需要的是一个很好的有序ActiveRecord集合,我可以这样显示吗? 书名 – 19票 书名 – 15票 书名 – 9票 书名 – 8票