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

Updating model with update_attribute doesn't work #25

Closed
jurezove opened this issue Oct 1, 2014 · 5 comments
Closed

Updating model with update_attribute doesn't work #25

jurezove opened this issue Oct 1, 2014 · 5 comments

Comments

@jurezove
Copy link

jurezove commented Oct 1, 2014

It looks like the id doesn't get deobfuscated before performing update_attributes.

I'm using:

bill = Bill.find(Bill.deobfuscate_id(params[:id]))
bill.update_attributes(bill_params)

and I get

ActiveRecord::StatementInvalid: PG::NumericValueOutOfRange: ERROR: value "4903661204" is out of range for type integer

@namick
Copy link
Owner

namick commented Feb 8, 2015

You never have to manually call deobfuscate_id.

Just make sure you include obfuscate_id in your model:

class Bill < ActiveRecord::Base
  obfuscate_id

  # model code
end

Then just do this:

bill = Bill.find(params[:id])
bill.update_attributes(bill_params)

Feel free to reopen the issue if you are still having a problem.

@namick namick closed this as completed Feb 8, 2015
@jurezove
Copy link
Author

jurezove commented Feb 8, 2015

I was encountering this when using CarrierWave and uploading attachments.

Not sure what the problem was but ended up doing a monkey patch and overriding #has_obfuscated_id? when uploading an attachment. :)

@scottbullard
Copy link

@jurezove can you elaborate on what you did to work around your issue by using has_obfuscated_id?

I am experiencing similar problems with trying to use obfuscate_id with carrierwave. No matter which method I use (find(obf_id) or find_by_id(orig_id)), carrierwave still has problems with an out of range value for 'id'

I had posted here a while back when at the time I believed it to be related to Resque, but it's also noted here quite some time ago but that fix referenced does not appear to work for this.

@jurezove
Copy link
Author

Hey @scottbullard sorry for the late reply. In the end, I did a small hack:

module HackableObfuscate
  def perform_deobfuscated
    result = false
    begin
      hack_class
      result = yield
    ensure
      unhack_class
    end
    result
  end

  private

  def hack_class
    self.class_eval do
      def self.has_obfuscated_id?
        false
      end
    end
  end

  def unhack_class
    self.class_eval do
      def self.has_obfuscated_id?
        true
      end
    end
  end
end

Then for example, called that method:

Payment.perform_deobfuscated { @payment.update_attributes(payment_params) }

Does something like this work for you?

@scottbullard
Copy link

@jurezove thanks - I will give that a try.

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

No branches or pull requests

3 participants