Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding trouble when creating an ActiveRecord object. #68

Closed
guilherme opened this issue Jul 25, 2011 · 4 comments
Closed

Encoding trouble when creating an ActiveRecord object. #68

guilherme opened this issue Jul 25, 2011 · 4 comments

Comments

@guilherme
Copy link

Hello,

I'm having issues trying to save my rails object.
The exception is:
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
My backtrace says:
[GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:69:in join' [GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:69:invisit_Arel_Nodes_Values'
[GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in visit' [GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:59:invisit_Arel_Nodes_InsertStatement'
[GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:15:in visit' [GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/visitor.rb:5:inaccept'
[GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:18:in block in accept' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:111:inwith_connection'
[GEM_ROOT]/gems/arel-2.0.10/lib/arel/visitors/to_sql.rb:16:in accept' [GEM_ROOT]/gems/arel-2.0.10/lib/arel/tree_manager.rb:20:into_sql'
[GEM_ROOT]/gems/arel-2.0.10/lib/arel/select_manager.rb:217:in insert' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/relation.rb:14:ininsert'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/persistence.rb:274:in create' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/timestamp.rb:47:increate'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/callbacks.rb:277:in block in create' [GEM_ROOT]/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:414:in_run_create_callbacks'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/callbacks.rb:277:in create' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/persistence.rb:250:increate_or_update'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/callbacks.rb:273:in block in create_or_update' [GEM_ROOT]/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:444:in_run_save_callbacks'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/callbacks.rb:273:in create_or_update' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/persistence.rb:40:insave'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/validations.rb:43:in save' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/attribute_methods/dirty.rb:21:insave'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:240:in block (2 levels) in save' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:292:inblock in with_transaction_returning_status'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in transaction' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:207:intransaction'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:290:in with_transaction_returning_status' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:240:inblock in save'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:251:in rollback_active_record_state!' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:239:insave'
app/controllers/lambs_controller.rb:245:in block in create' [GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/connection_adapters/abstract/database_statements.rb:139:intransaction'
[GEM_ROOT]/gems/activerecord-3.0.9/lib/active_record/transactions.rb:207:in transaction' app/controllers/my_controller.rb:229:increate'

i'm using ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux] and adjusted the encoding as utf-8 either on database.yml and my_controller.rb with (#encoding:utf-8)

How to solve this issue?

thank you.

@tenderlove
Copy link
Member

I guess you're storing an image or something along with some UTF-8 data in the database? Is that correct?

@guilherme
Copy link
Author

yes! what should i do?

@guilherme guilherme reopened this Aug 4, 2011
@guilherme
Copy link
Author

@tenderlove, could you help me to fix this bug?

tell me where i look, how it could be solved this issue.. i could make the code and pull request.

thanks

@tenderlove
Copy link
Member

You cannot concatenate UTF-8 and binary data. ARel is building a SQL statement, and that is exactly what is happening. In order to solve this, you have three choices:

  1. Upgrade to Rails 3.1 so that it uses prepared statements
  2. Encode the data with something the database understands (like base64)
  3. Use two sql statements, one inserts the UTF-8 data, the other does an update with only ASCII + Binary.

This is not an arel bug (unfortunately).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants