2

We are encrypting values in table / model - RegisteredDomain, using attr_encrypted below

attr_encrypted :domain_name, :registered_by, :api_key, :key => "65xU4TdZntTV53"

Values get encrypted and stored to db as well. Below code saves values to db.

registereddomain = RegisteredDomain.new(
        :domain_name => domain_name,
        :api_key =>  api_key,
        :hash_key => hash_key,
        :registered_by => ep_id,
        :status =>  status,
        :domain_type_id => domain_type_id              
        )
registereddomain.save

Problem is with decrypting values with below select. We get encrypted values here, could anyone tell how to get decrytpted values in listing.

def select_all
  @registered_domains = RegisteredDomain.select("id, encrypted_domain_name, domain_type_id, encrypted_api_key, status").order(updated_at: :desc)
  return @registered_domains
end
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
Sashant Pardeshi
  • 1,075
  • 7
  • 21
  • 1
    Could you please put your schema file, where `RegisteredDomain` is described. That will give me an answer. As for me, it seems that you only pulling the encrypted_ columns and not the actual columns – Dan Myasnikov Oct 26 '15 at 11:19

2 Answers2

0

There is probably a reason to store values in the database in encrypted mode. Hence if you want to decrypt the value, just call the appropriate method on an instance method:

@registered_domains = RegisteredDomain.select("id, encrypted_domain_name, domain_type_id, encrypted_api_key, status").order(updated_at: :desc)
@registered_domains.first.domain_name

Edit

you migth want to create a custom method for pulling the values from the database:

def self.all_decrypted(columns)
  all.map do |record|
    columns.map do |column|
      record.send(column.to_sym)
    end
  end
end

RegisteredDomain.all_decrypted([:domain_name, :api_key]) This will return you an array of arrays, actual values of domain_name, api_key.

0

It'll be good if you can give some sample code of yours for controller and view as well.. As I am seeing you are directly using fields without the attribute option; so in your database the field domain_name mus be represented as encrypted_domain_name. So here in your select query you can try to modify your the attribute encrypted_domain_name to domain_name. Also you should modify it where-ever you want to get the encrypted attribute. (I am not able to write the complete code-block properly hence I tried to explain it in short; but just renaming the attribute should work!)

Swaps
  • 1,450
  • 24
  • 31
  • thnx man , We have resolved the issue using iteration on object we can get listing now. @registered_domains.each do | registered_domain | registered_domain.domain_name end – Sashant Pardeshi Dec 07 '15 at 13:30