Rails 3 - Awesome Nested Set - Comment.rebuild! - Results in a ERROR - ruby-on-rails

I'm using the awesome_nested_set rails 3 fork here: https://github.com/FreakyDazio/awesome_nested_set
I'm trying to rebuild the nesting per the installation instructions.
I went to console and typed in:
>>Comment.rebuild!
But that errors with:
ActiveRecord::StatementInvalid: PGError: ERROR: column "comments.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: HAVING COUNT("lft") > 1 ORDER BY comments.cr...
^
: SELECT "lft", COUNT("lft") FROM "comments" GROUP BY "lft"
HAVING COUNT("lft") > 1 ORDER BY comments.created_at DESC LIMIT 1
Any thoughts or ideas why?
Thanks

This same issues is reported in another fork of acts_as_nested_set (you seem to have all the bad luck. :) See this error report. He does mention a workaround you can put in your model:
def self.all_roots_valid?
if acts_as_nested_set_options[:scope]
roots.group_by{|record| scope_column_names.collect{|col| record.send(col.to_sym)}}.all? do |scope, grouped_roots|
each_root_valid?(grouped_roots)
end
else
each_root_valid?(roots)
end
end

Related

How do I decrypt in rails?

Hello fellow developers,
I have been stuck on this isssue for quite sometime now. All I want to do is somehow decrypt a column (ssn_or_ein) in my table (candidates) which was previously encrypted by the gem crypty_keeper.
https://github.com/jmazzi/crypt_keeper
What i have tried so far:
required the gem in Rails C and used the decrypt_table! against my Model
However, I am unable to catch the method
[pry(main)> Candidate.decrypt_table!
(0.2ms) BEGIN
Load (12.1ms) SELECT "candidates".* FROM "candidates" ORDER BY "candidates"."id" ASC LIMIT $1 [["LIMIT", 1000]]
(4.8ms) ROLLBACK
NameError: undefined local variable or method `crypt_keeper_fields' for #<Class:0x0000000007c5f038>
Did you mean? crypt_keeper
Also tried putting the pre-existing code back in my Application
class Candidate < ApplicationRecord
crypt_keeper :gateway_token, :ssn_or_ein, :e_in, encryptor: :active_support, key: ENV['ENCRYPTION_KEY'], salt: ENV['ENCRYPTION_SALT']
and I get an Active Support error
pry(main)> Candidate.decrypt_table!
(0.3ms) BEGIN
Load (609.8ms) SELECT "candidates".* FROM "candidates" ORDER BY "candidates"."id" ASC LIMIT $1 [["LIMIT", 1000]]
(0.3ms) ROLLBACK
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage
from /home/niketa/.rvm/gems/ruby-2.6.7#raisethemoney5.2.5/gems/activesupport-5.2.6/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt'
Caused by ArgumentError: invalid base64
This is the error i am getting.
Any help or insight would be appreciated. Thanks
It looks like you have rows in your table that are not encrypted and those cause the Candidate.decrypt_table! call to fail. Did you maybe remove the crypt_keeper line from the Candidate class and then add entries? If so you'll have to remove them or skip them...

Error in an after hook, PG::InFailedSqlTransaction from Rspec

I was trying to run rspec from a model spec file, but I got this error: "An error occurred in an after hook"
"An error occurred in to after hook PG :: InFailedSqlTransaction:
ERROR: current transaction is aborted, commands ignored until end of
transaction. occurred at C :/ Ruby193/lib/.../postgresql_adapter: 294 "
I googled this issue, and I found a suggestion to downgrade my 'database_cleaner' to '1.0.1'. I did, but it doesn't work.
Does anyone have any idea how to solve this? Thanks in advance!
This can happen if you execute a bad SQL statement in the scope of a transaction, you rescue the exception from that statement, and then try and execute another SQL statement in the same transaction.
Once one statement in a transaction fails no more statements can be executed in that transaction.
Here's an example:
ActiveRecord::Base.transaction do
begin
ActiveRecord::Base.connection.execute "A bad query"
rescue => ex
puts ex.message
end
puts User.count
end
User.count raises PG::InFailedSqlTransaction because the previous SQL statement raised ActiveRecord::StatementInvalid and that was swallowed by the rescue.
So I would look for code that rescues in the scope of a transaction and then tries to run additional SQL statements.

Postgres throws error when running import from activerecord-import gem

I switched to Postgres in development and now following error breaks a test case:
PG::Error: ERROR: column "id" of relation "assets_content_nodes" does not exist
: SELECT pg_get_serial_sequence($1, $2)
Same error happens in development and production environments but it is not a show stopper and does not affect the import.
assets_content_nodes is a join table and I'd rather not add the 'id' column.
Any ideas why this may be happening?
This is fixed in activerecord-import -v0.3.1 (or possibly an earlier version, but it was broken in 0.2.11 and earlier). You no longer need an id column on a table to do an import.
You might also try Upsert.
If you're importing pets:
require 'csv'
require 'upsert'
u = Upsert.new Pet.connection, Pet.table_name
CSV.foreach('pets.csv', headers: :first_row) do |row|
selector = { name: row['name'] }
setter = row
u.row selector, setter
end
My tests show it's 20–30% faster than activerecord-import. Since it's "upserting," it's generally OK if the process gets interrupted and you just start over from the beginning.

Intermittent PostgreSQL error: missing FROM-clause

I have a Rails app running on Heroku which intermittently throws Postgres 'missing FROM-clause' errors from UPDATEs executed by a Rake task.
An example UPDATE statement is:
UPDATE "temp_stats_values"
SET "number" = 66,
"average" = 0,
"percentage" = 0,
"num_out_of" = 0,
"updated_at" = '2013-01-12 11:43:40.067530'
WHERE "temp_stats_values"."id" = 1330
And the error thrown is:
ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table "temp_stats_values"
The thing is, I don't know why it's expecting a FROM clause for this SQL when there clearly isn't another table involved. Also, the fact that it seems to be intermittent is pretty confusing. The errors, when they happen, are being caught by Airbrake.
Anyway, if anyone has any good ideas I'd be very grateful.
UPDATE
Example SQL Fiddle which works as expected
So, without any intervention these errors magically dried up, just like they magically started happening in the first place. Spooky. Anyway, thanks to all who commented.

to_xml always throws error

I'm on Rails 3.1
I've pasted an example below, but Calling to_xml on any model throws this error. I've tried everything but I'm it's the sort of thing someone would have seen before. Does it ring any bells for anyone?
1.8.7 :004 > Person.first.to_xml
Person Load (2.0ms) SELECT `people`.* FROM `people` LIMIT 1
ArgumentError: wrong number of arguments (1 for 0)
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlbase.rb:135:in `to_xs'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlbase.rb:135:in `_escape'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlbase.rb:140:in `_escape_quote'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:320:in `_attr_value'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:308:in `_insert_attributes'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:306:in `each'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:306:in `_insert_attributes'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:284:in `_special'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/builder-3.0.0/lib/builder/xmlmarkup.rb:250:in `instruct!'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/activemodel-3.1.1/lib/active_model/serializers/xml.rb:93:in `serialize'
from /home/khughes/.rvm/gems/ree-1.8.7-2012.02#rails31/gems/activerecord-3.1.1/lib/active_record/serializers/xml_serializer.rb:175:in `to_xml'
from (irb):4
had the same issue some time ago, do you have hpricot gem bundled into the project? if so, try to remove it from the Gemfile, run bundle and check if to_xml works
there's a solution that worked for me:
https://github.com/hpricot/hpricot/issues/53 ( see smeevil's post )

Resources