rails-activerecord

Ruby ActiveRecord and sql tuple support

一曲冷凌霜 提交于 2019-12-14 00:23:40
问题 Does ActiveRecord support tuples in the where clause, assuming the underlying database does? The resulting where clause would look something like: where (name, address) in (('John', '123 Main St')) I tried: Person.where({[:name, :address] => ['John', '123 Main St']}) and it didn't work. 回答1: tupleArray = [['John', '123 Main St'],['Jane', '124 Main St']] Person.where("(name, address) IN (#{(['(?)']*tupleArray.size).join(', ')})", *tupleArray) 回答2: Person.where("(name, address) IN ((?))", [

Cannot use group with has_many through in Rails 5

杀马特。学长 韩版系。学妹 提交于 2019-12-13 20:27:01
问题 I have the following associations: class Student < ApplicationRecord has_many :people_schools has_many :schools, through: :people_schools end class PeopleSchool < ApplicationRecord belongs_to :student belongs_to :school end class School < ApplicationRecord has_many :people_schools has_many :students, through: :people_schools end I am trying to get a list of students organized by their school. I have tried the following: Student.joins(:schools).all.group('schools.name') but I get the following

ruby each loop in order on sql order query result

匆匆过客 提交于 2019-12-13 20:19:19
问题 I have a query in my Controller that works perfectly: @klasses_mon = Klass.order(:start).where(day: 'MON').find_each my result is (shown by <%= @klasses_mon.inspect %> in my view): #<Enumerator: #<ActiveRecord::Relation [#<Klass id: 9, name: "Cycling", teacher: "Tomek", day: "MON", start: 510, duration: 45>, #<Klass id: 8, name: "LBT", teacher: "Monia", day: "MON", start: 600, duration: 60>, #<Klass id: 11, name: "HIIT", teacher: "Aga", day: "MON", start: 930, duration: 45>]> :find_each({

Moving transactional operations away from the controller

非 Y 不嫁゛ 提交于 2019-12-13 19:19:11
问题 def cancel begin to_bank = @transfer.main_to_bank to_bank.with_lock do to_bank.locked_balance -= @transfer.amount to_bank.available_balance += @transfer.amount to_bank.save! @transfer.cancel @transfer.save! end rescue ActiveRecord::ActiveRecordError => e redirect_to admin_transfer_url(@transfer), alert: "Error while cancelling." return end redirect_to admin_transfer_url(@transfer), notice: 'Transfer was successfully cancelled.' end I would want to refactor the above code to the Transfer model

Active record in standalone Ruby

假装没事ソ 提交于 2019-12-13 15:17:57
问题 I have standalone Ruby application and want to use it with active record gem. I've made 3 models: user.rb require 'active_record' class User < ActiveRecord::Base has_many :posts, :dependent => :destroy has_many :comments validates :name, :presence => true attr_accessible :name, :state end post.rb require 'active_record' class Post < ActiveRecord::Base belongs_to :user has_many :comments validates :title, :length => { :in => 6..40 } attr_accessible :title, :content end comment.rb require

Displaying attributes for associated models in views

痴心易碎 提交于 2019-12-13 12:31:40
问题 I want to fetch username or email( both are in user table) of user who creates article in blog application. Currently I am able to fetch user id from articles_controller.rb def create @article = Article.new(params[:article]) @article.user_id = current_user.id @article.save redirect_to article_path(@article) end but no idea how to fetch username or email for same. Basically I want to display username or email on article index page. Please suggest me to how to get done it user.rb class User <

Navigating through relations on two paths in schema

Deadly 提交于 2019-12-13 08:47:06
问题 From top to bottom I have the belongs_to relationship between my tables , and well has_many from the other direction. ReportTarget Report Manager Organization and Score Manager Organization so notice that Report table and Score table are kind of on the same level. They both have Manager Table as their parent. Individually I could figure out how to navigate them with eager loading. For first one I will do: @blah = Organization.includes(managers: { reports: :report_targets }).find(params[:id])

Writing scope, join and order for a Model

只愿长相守 提交于 2019-12-13 07:21:03
问题 My model is like this: Program has_many Measures and then Measures has_many Targets and Target table has a column named value My query is like this: @programs2 = Program.includes([measures: :targets]) .some_scope .where('organization_id = 1') .limit(2) I don't know where or how to write the some_scope part of the query. The query starts with Program.includes so I think it should be defined in the Program model but the problem I have is that measures: :targets . How do I define a join for them

Does reaper work with oracle-enhanced adapter (OCI) to reap out idle connections?

荒凉一梦 提交于 2019-12-13 06:25:20
问题 I am trying to play with connection pooling with activerecord in Ruby. I am using Oracle-enhanced adapter and setting the following properties in database.yml: reaping_frequency: 30 dead_connection_timeout: 20 I don't see my idle connections getting reaped. I am using Rails3. I think reaper is available in Rails4. 回答1: As you say Reaper is only available in Rails 4. However, even with rails 4, oracle-enhanced adapter still does not work with reaper. There is an open ticket with oracle

Postgresql: comparing arrays results in “malformed array literal” error

ⅰ亾dé卋堺 提交于 2019-12-13 05:34:08
问题 I have a Rails app and a column that contains an array like ["manager", "engineer"] etc. and a where statement like this: where("? = ANY roles", query) which works find if I pass a single value for query. I want to be able to pass multiple values. I did some Googling and an simple solution was found: where("? && roles", query ) except that if I pass something in like "['admin', 'guest']" I get this error: PG::InvalidTextRepresentation: ERROR: malformed array literal: "['admin', 'guest']" LINE