Tag: postgresql

搜索表单没有路由到Rails 5.1中的适当控制器

我在这一天上已经挣扎了几天: 我的导航栏中有一个搜索表单,通过作业模型在我的Users表的:address列中运行搜索。 这是我的JobsController的索引方法: def index @jobs = if params[:address] Job.joins(:user).where(users: { address: params[:address].downcase }).paginate(page: params[:page], per_page: 3) else @jobs = Job.paginate(page: params[:page], per_page: 3).order(‘id DESC’) end end 这是表格: 这是我的路线档案: Rails.application.routes.draw do devise_for :users get ‘static_pages/home’ get ‘static_pages/faq’ get ‘static_pages/about’ get ‘static_pages/cgu’ root ‘static_pages#home’ get ‘/faq’, to: ‘static_pages#faq’ get ‘/about’, to: ‘static_pages#about’ get ‘/contact’, to: ‘static_pages#contact’ […]

如何找到* all *匹配类别的项目

我有两个模型,Item和Category,由连接表连接。 我想查询Item以仅查找与类别列表匹配的项目。 我的模型看起来像: class Item < ActiveRecord::Base has_and_belongs_to_many :categories end class Category < ActiveRecord::Base has_and_belongs_to_many :items end 我可以轻松找到与任何类别列表匹配的项目。 以下将返回属于类别1,2或3的项目。 Item.includes(:categories).where(categories: {id:[1,2,3]}) 我只想找到属于所有3个类别的项目。 使用ActiveRecord实现此目的的最佳方法是什么? 我是否需要回退自己编写where条件,如果是这样,PostgreSQL的正确语法是什么? 我尝试了各种各样的“WHERE ALL IN(1,2,3)”,但只是语法错误。 更新: 基于查找产品匹配所有类别(Rails 3.1)的接受答案,我可以非常接近。 category_ids = [7,10,12,13,52,1162] Item.joins(:categories). where(categories: {id: category_ids}). group(‘items.id’). having(“count(categories_items.category_id) = #{category_ids.size}”) 不幸的是,当链接.count或.size我得到一个Hash而不是记录计数: {189 => 6, 3067 => 6, 406 => 6} 我可以计算结果哈希中的键来获得真实的记录数,但这是一个非常不优雅的解决方案。

如何使用rails项目配置Postgresql?

我对ruby很新。 我正在尝试运行BBYIDX并按照如何在Windows上安装BBYIDX? 当我尝试rake db:migrate它会重现错误 (in /home/virinchy/BBYIDX-2/BBYIDX-master) DEPRECATION WARNING: Rake tasks in vendor/plugins/acts_as_tsearch/tasks, vendor/plugins/acts_as_tsearch/tasks, vendor/plugins/delayed_job/tasks, vendor/plugins/nested_scenarios/tasks, vendor/plugins/rails-authorization-plugin/tasks, and vendor/plugins/rails_rcov/tasks are deprecated. Use lib/tasks instead. (called from /usr/local/rvm/gems/ruby-1.8.7-p371/gems/rails-2.3.11/lib/tasks/rails.rb:10) rake aborted! FATAL: password authentication failed for user “bbyidx” FATAL: password authentication failed for user “bbyidx” Bundle install was succesful with message as:Your bundle is complete! Use `bundle show […]

从其json表示中获取IPAddr实例

如何从json表示中获取IPAddr实例? 语境: 我正在使用一个使用draftsmangem的Rails应用程序。 基本上,当一个人对数据库中的记录和ruby模型has_drafts进行更改时,不会立即对记录进行更改,而是将更改草稿和结果记录保存在草稿表中,直到一次publish!为止publish! 那个草案。 然后对实际记录进行更改。 很简约 :) 草稿表保存了允许更改发生在postgres表中的json列中的对象。 我也使用Devise,我的用户模型使用trackable模块,其中包括current_sign_in_ip以及列类型inet的last_sign_in_ip 。 问题: 我的用户表中的inet类型列以及User对象的其余部分存储在由draftsman gem在名为object的单个json列中创建的草稿表中。 由于以下原因,在这种情况下从对象中重新定义对象不成功: IPAddr实例具有IPAddr类无法感知的json表示。 例如,ruby中的IPAddr实例响应to_json ,这给我一些如下所示的内容: “\”family\”:2,\”addr\”:2130706433,\”mask_addr\”:4294967295}” 。 这些实例也响应to_s并给我这样的东西: “127.0.0.1” 。 不幸的是, IPAddr类似乎不知道如何从我所知道的json中恢复过来: [277, 286] in /home/jake/.rvm/gems/ruby-2.2.1/bundler/gems/draftsman-1bd3c797d540/lib/draftsman/model.rb 277: data = { 278: :item => self, 279: :whodunnit => Draftsman.whodunnit, 280: :object => object_attrs_for_draft_record 281: } => 282: data = merge_metadata_for_draft(data) 283: 284: # […]

范围:按两个日期之间的差异排序

我想创建一个范围,从最近的日期到最远的日期对数据进行排序。 例如,我有3个值: 然后我想对最接近给定日期的日期进行排序: 2012-12-29 。 我应该得到这个顺序: 2, 3, 1 。 如果我选择2012-12-30 ,结果必须是: 2012-12-30 。 我试过这样的事情: scope :order_by_closest_date, lambda{|time| select(“*, (date – DATE(‘#{time}’)) AS time”).order(“time ASC”) } 但它不起作用。 有关信息:Rails 3.2.9 Ruby 1.9.3 Postgresql 9.1.4。 有任何想法吗?

ActiveRecord模糊搜索

我正在寻找搜索以找到类似于此的对象: Object(id: 1, example: “abc”) 通过使用这样的搜索: params[:search] = “abcdef” Object.where(“example LIKE ?”, “#{params[:search]%”) 但我只能使用上面的例子,如果我的搜索字符少于我的对象,而不是更多。

Rails连接到database.yml指定的数据库

我最近重启了战俘。 在重新加载我正在处理的Rails应用程序时,它挂了。 检查Rails的日志我每隔5-20秒重复一次: Connecting to database specified by database.yml 我可以通过Rails控制台毫无问题地与Postgres数据库进行交互,因此没有问题。 我在我的database.yml中没有改变任何东西几周,所以我认为问题不在那里。 可能是什么问题,我该如何调试它?

如何覆盖Ruby Ranges的..和…运算符以接受Float :: INFINITY?

我想覆盖Ruby的Range的..和…运算符。 原因是,我正在使用数据库中的无限日期范围。 如果你从Postgres中infinty一个infinty日期时间,你会在Ruby中得到一个Float::INFINITY 。 这个问题是,我不能使用Float::INFINITY作为范围的结尾: Date.today…Float::INFINITY => Wed, 02 Nov 2016…Infinity DateTime.now…Float::INFINITY # ArgumentError: bad value for range Time.now…Float::INFINITY # ArgumentError: bad value for range …但我在代码中经常使用..和…语法。 为了能够构造范围,您需要使用DateTime::Infinity.new : Date.today…DateTime::Infinity.new => Wed, 02 Nov 2016…# DateTime.now…DateTime::Infinity.new => Wed, 02 Nov 2016 12:57:07 +0000…# Time.now…DateTime::Infinity.new => 2016-11-02 12:57:33 +0000…# 但我每次都需要进行Float::INFINITY – > DateTime::Infinity.new转换: model.start_time…convert_infinity(model.end_time) 有没有办法可以覆盖..和…运算符,以便我可以合并转换函数并保持语法糖?

Gem :: LoadError:为数据库适配器指定’postgresql’,但未加载gem

帮我! 输入“mina deploy”得到这个美: 我的“/home/deploy/inscope/shared/config/database.yml”看起来像: production: adapter: postgresql encoding: unicode database: *** username: *** password: *** host: localhost rake aborted! Gem::LoadError: Specified ‘postgresql’ for database adapter, but the gem is not loaded. Add `gem ‘pg’` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). /home/deploy/inscope/tmp/build-142737137923106/vendor/bundle/ruby/2.2.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:190:in `rescue in spec’ /home/deploy/inscope/tmp/build-142737137923106/vendor/bundle/ruby/2.2.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:187:in `spec’ /home/deploy/inscope/tmp/build-142737137923106/vendor/bundle/ruby/2.2.0/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:50:in […]

在rails上的ruby中将sqlite更改为PostgreSQL

我正在使用win 7与docker和docker我在轨道上使用ruby,我做了一个适用于sqlite3的应用程序但是当我尝试上传到Heroku时,Heroku给了我一个错误,它不支持sqlite3所以我有将我的app数据库更改为PostgreSQL并进行了以下更改:在我的Gemfile中我评论了gem sqlite3: # Use sqlite3 as the database for Active Record #gem ‘sqlite3’ #posgresql gem ‘pg’ 第二个我做了“捆绑安装”,然后是docker-compuse构建 然后在我的database.yml中: 之前: default: &default adapter: sqlite3 pool: timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do […]