Ruby on Rails - devise - User has :email field after removing it - ruby-on-rails

GDPR compliance is a must have for my application. The only sensible data that I need to keep are email adresses, so I used https://github.com/ziptofaf/gdpr-rails as my base.
However, in my Rails console after -> User.connection -> User -> It results in this User(id: integer, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, username: string, encrypted_email: string, encrypted_email_iv: string, avatar_file_name: string, avatar_content_type: string, avatar_file_size: integer, avatar_updated_at: datetime, email: ) it keeps saying that I have a email field. I checked my database tables and a email table doesn't exist.
The base comes with a bunch of methods and I want to provide my Users a way to export the data that I keep of them.
<%= JSON.parse(User.export_personal_information_from_model(#user.id))%>
This results in seeing my acutal email unencrypted within the :email field from above. If I check this explicit User via Rails console the :email field says email: nil
{"id"=>3, "created_at"=>"2018-07-26T21:22:22.763+02:00", "updated_at"=>"2018-07-26T21:22:22.242+02:00", "username"=>"username", "encrypted_email"=>"somerandomstring=\n", "encrypted_email_iv"=>"somerandomstring\n", "avatar_file_name"=>nil, "avatar_content_type"=>nil, "avatar_file_size"=>nil, "avatar_updated_at"=>nil, "email"=>"some#email.com"}
Update:
Migration to remove column from users
class RemoveEmailFromUser < ActiveRecord::Migration[5.1]
def change
remove_column :users, :email, :string
end
end
Anyone know what is going on here?
Thank you!

(After discussion in the chat...)
There isn't an email column, but there is an email attribute on the model. This is by design.
The GDPR-Rails gem works by defining columns named: encrypted_email and encrypted_email_iv, which store the encrypted email and the encryption key.
Then, when you call user.email, it uses this information to decrypt the email address.
Calling User.export_personal_information_from_model(#user.id) generates JSON of the record's attributes - which (again, by design) includes the decrypted email address.
So, tl;dr: Everything was working as it should, there was just a misunderstanding about the data being stored as encrypted.

Related

Adding Custom Attribute In Audited Record

I want to add custom attributes in the Audit Log like first_name, last_name using audited gem. Any idea how I can update the values of my custom attributes ?
#<AuditLog id: 1,
auditable_id: 2,
auditable_type: "User",
associated_id: nil,
associated_type: nil,
user_id: 2,
company_id: nil,
user_type: "User",
username: nil,
first_name: nil, last_name: nil, action: "update",
audited_changes: {"first_name"=>["Name1", "Name2"]}, version: 8,
comment: nil,
remote_address: "::1", request_uuid: "ed24f0d3-7ca7-42b1-b1a4-202222b38f7c", created_at: "2022-01-28 17:59:08">
enter code here
My user model contains these two attributes
t.column :first_name, :string
t.column :last_name, :string
It should just work. The audit record doesn't store discrete fields, it stores a YAML or jsonb representation of all changed fields. If you've added first_name to your users table, the audit record will automatically note any changes to that attribute.

rails active records returning wrong class type

i created a new model in rails with the following commands:
rails g model prod_domain name:string type:string user_logon_name:string description:string email:string address:string company:string department:string dn:string sa_description:string is_sa:string sa_remap_description:string ownership:string comment:text
rake db:migrate
i added some function in the model like this:
class ProdDomain < ActiveRecord::Base
def self.search_by_id(keyword)
users = Array.new
ProdDomain.where(id: keyword).find_each do |user|
users.push(user)
end
return users
end
def self.search(keyword)
users = []
ProdDomain.where(dn: keyword).find_each do |user|
users.push(user)
end
users
end
end
however, when i try to find records in this model, i kept getting 'USER' class instead of 'ProdDomain' class, what am i doing wrong:
(byebug) ProdDomain.find(1).class
ProdDomain Load (0.4ms) SELECT prod_domains.* FROM prod_domains WHERE prod_domains.id = 1 LIMIT 1
User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, username: string, role: integer, dn: string, department: string, name: string)
It looks like you have a type column defined. In Rails the type column is used by default for indicating inheritance.
If you're not intending on using inheritance then you can rename the column (something like kind is common) or you can overwrite the column name that Rails will use Base.inheritance_column.

search by elasticsearch-model not returns the records

I'm using elasticsearch-model and I used concern approach and here is my search method.I have tried my query by curl it returns me back 5 entries but when I used this method
def search_index(*args)
self.__elasticsearch__.search(*args)
end
it's returns me a class
(byebug) Entry.search_index('test').records
#<Elasticsearch::Model::Response::Records:0x007f8eb85ca280 #klass=[PROXY] Entry(id: integer, created_at: datetime, updated_at: datetime, source_id: integer, data: text, uuid: string, source_entry_id: string, bytesize: integer), #response=#<Elasticsearch::Model::Response::Response:0x007f8eb85ca370 #klass=[PROXY] Entry(id: integer, created_at: datetime, updated_at: datetime, source_id: integer, data: text, uuid: string, source_entry_id: string, bytesize: integer), #search=#<Elasticsearch::Model::Searching::SearchRequest:0x007f8eb85ca438 #klass=[PROXY] Entry(id: integer, created_at: datetime, updated_at: datetime, source_id: integer, data: text, uuid: string, source_entry_id: string, bytesize: integer), #options={}, #definition={:index=>"self_driving_entries", :type=>"entry", :q=>"test"}>, #records=#<Elasticsearch::Model::Response::Records:0x007f8eb85ca280 ...>>, #options={}>
how can I have access to my records by this search method ?
You should use to your
respond
records
Like Example
Foo.new().search('1234').response.records
It will return your Active record objects.

How to "pluck" an image field in Rails

My Model "Appimage" has the following fields
AppImage(id: integer, content: text, app_image_file_name: string, app_image_content_type: string, app_image_file_size: integer, app_image_updated_at: datetime, created_at: datetime, updated_at: datetime)
Consider it has five records. Now, I want to fetch all the values of app_image field (in url format) like we do ModelName.pluck(:some_field_name).
If I fetch an URL for the first record AppImage.first.app_image.url, it returns as follows
http://s3-amazonaws.com/my_app/attachments/3/original/file_name.jpg?1446218644
Please help me if you have idea about it.

Rails split string value as group of object

I currently receive from a form a string who looks like that :
one tag,another tag,yet another
Inside my Rails app I implemented a polymorphic association :taggable in order to manage tag (taxonomy) on my system. What I want now is to save those value in the database with of course, the less code as possible. My tables structure looks like that :
// Assocition table
TermRelationship(id: integer, taggable_id: integer, taggable_type: string, created_at: datetime, updated_at: datetime)
// Term table (tag)
Term(id: integer, name: string, created_at: datetime, updated_at: datetime, type: string)
// Blog table
Blog(id: integer, title: string, body: text, created_at: datetime, updated_at: datetime)
So far what I did is to split those value as an array :
tags = params[:blog][:tag].split(/,/)
Now the only I can think of is to have a loop in order to add one by one each value. But Im sure there is another "Rails/Ruby"-like to do it.
Thank you for your help
Do not reinvent the wheel. Take a look at https://github.com/mbleigh/acts-as-taggable-on

Resources