activerecord

Rails ignores constants in SQL SELECT statement

血红的双手。 提交于 2020-01-21 19:49:09
问题 Is there a trick required to make Rails recognize constants in a SQL select statement? For example, the following SQL statement is valid: SELECT id, name, 1 AS constant FROM table_name And I would expect the results to have three columns returned: id , name and constant . The value in the constant column would always be 1. However, in Rails if I try to do the same thing the constant column gets dropped using Model.find_by_sql : TableName.find_by_sql("SELECT id, name, 1 AS constant FROM table

Rails 3. Nested transactions. Exception in a child block

ぐ巨炮叔叔 提交于 2020-01-21 12:46:50
问题 Why doesn't ActiveRecord rollback changes in nested transactions after exception was risen in a child block? Here are examples: 1. >> Client.transaction do ?> Client.create(:name => 'Pavel') >> Client.transaction do ?> Client.create(:name => 'Elena') >> raise ActiveRecord::Rollback >> end >> end => nil >> Client.all.map(&:name) => ["Pavel", "Elena"] # instead of [] 2. >> Client.transaction do ?> Client.create(:name => 'Pavel') >> Client.transaction(:requires_new => true) do ?> Client.create(

Rails 3. Nested transactions. Exception in a child block

蹲街弑〆低调 提交于 2020-01-21 12:46:17
问题 Why doesn't ActiveRecord rollback changes in nested transactions after exception was risen in a child block? Here are examples: 1. >> Client.transaction do ?> Client.create(:name => 'Pavel') >> Client.transaction do ?> Client.create(:name => 'Elena') >> raise ActiveRecord::Rollback >> end >> end => nil >> Client.all.map(&:name) => ["Pavel", "Elena"] # instead of [] 2. >> Client.transaction do ?> Client.create(:name => 'Pavel') >> Client.transaction(:requires_new => true) do ?> Client.create(

Rails query timestamp between two hours

拟墨画扇 提交于 2020-01-21 05:55:41
问题 I have a problem in Ruby on Rails, where I need to allow a user to set a two time columns, and then query all instances of that model that have the current time within the hours of the two timestamps. Days do not matter, just within the times specified be the start and end timestamps. Thanks, Brian 回答1: Something like this should work (assuming MySQL): Model.where("TIME(created_at) BETWEEN '07:00:00' AND '09:00:00'") You can convert a Time instance to this format with: Time.now.strftime("%T")

Return entity via projection query

扶醉桌前 提交于 2020-01-21 05:29:07
问题 Is it possible to return an entity using a projection query? I've successfully done it with a SQL query (see below), but can't find how to do it with a projection query. Dim sql As String = "SELECT {a.*}, {b.*} FROM a LEFT OUTER JOIN b ON a.pk = b.fk") ' Convert SQL results into entities {a} and {b} Dim query As IQuery = session.CreateSQLQuery(sql) _ .AddEntity("a", GetType(a)) _ .AddEntity("b", GetType(b)) Return query.List() 回答1: Yes, you can return entities from projection queries. If you

Ruby on Rails memory leak when looping through large number of records; find_each doesn't help

泪湿孤枕 提交于 2020-01-20 17:10:46
问题 I have a Rails app that processes a large (millions) number of records in a mysql database. Once it starts working, its memory use quickly grows at a speed of 50MB per second. With tools like oink I was able to narrow down the root cause to one loop that goes through all the records in a big table in the database. I understand if I use something like Person.all.each , all the records will be loaded into memory. However if I switch to find_each , I still see the same memory issue. To further

Ruby on Rails memory leak when looping through large number of records; find_each doesn't help

旧城冷巷雨未停 提交于 2020-01-20 17:09:13
问题 I have a Rails app that processes a large (millions) number of records in a mysql database. Once it starts working, its memory use quickly grows at a speed of 50MB per second. With tools like oink I was able to narrow down the root cause to one loop that goes through all the records in a big table in the database. I understand if I use something like Person.all.each , all the records will be loaded into memory. However if I switch to find_each , I still see the same memory issue. To further

Why are Rails model association results not naturally ActiveRecord::Relations?

跟風遠走 提交于 2020-01-20 03:59:08
问题 I'm using Rails 3.2.0 Let's say I have: class Comment < ActiveRecord::Base has_many :articles end c1 = Comment.last then c1.articles.class # => Array c1.articles.where('id NOT IN (999999)').class # => ActiveRecord::Relation Why is the result of an association not a type of ActiveRecord::Relation ? It clearly is / was at some point : c1.articles.to_orig # undefined method `to_orig' for #<ActiveRecord::Relation:0x007fd820cc80a8> c1.articles.class # => Array Certain evaluations act upon an

How to run specific script after connected to oracle using rails?

穿精又带淫゛_ 提交于 2020-01-17 08:17:31
问题 I need to run an oracle script after connect to oracle database using ActiveRecord. I know that exists the initializers, but these run only in the application's start. I need a point to write a code that runs after every new database connection be established. This is needed to initialize some oracle environments variables shared with others applications that uses the same legacy database. Any ideas? Thanks in advance. 回答1: I found the solution: Create the file /config/initializers/oracle.rb

Heroku + Rails Error ActiveRecord::StatementInvalid

流过昼夜 提交于 2020-01-17 07:43:11
问题 I'm having a bit of a dilemma here. I have a Comments scaffold and a Movies scaffold When going to a movie page and creating a new comment, i go here /movies/12/comments/new (12 being the id) And did is my controller def new @movie = Movie.find(params[:movie_id]) @comment = @movie.comments.new @search = Movie.search(params[:q]) respond_to do |format| format.html # new.html.erb format.json { render json: @comment } end end But when i launch to heroku I get this error *Processing by