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.
Related
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.
I've got a custom Spree app. I need to query Spree::Products that have the associated Spree::Property
I have a 'property' with the name "Rating" on only certain products, but I can't query those products correctly. What I have now is:
Spree::Product.joins(:properties).where(:property_name.downcase == "rating")
but that just pulls all the products that have any :properties associated with them at all.
Spree::Property -
Spree::Property(id: integer, name: string, presentation: string, created_at: datetime, updated_at: datetime)
Just tested on Spree 3.1. You can do that from Product or Property model.
Spree::Product.joins(:properties).where(spree_properties: {name: "rating"})
or
Spree::Property.where(name: "rating").first.products
*You have to modify the code to use downcase string.
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.
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
I am using audited to track changes for a model called Page. I would like to be able to find all audits associated with a certain user (via user_id in the audits table).
How can I do that? So far, the only way I have found to access the Audit model is like this:
#audits = Audited::Adapters::ActiveRecord::Audit.all
Which just doesn't seem like it's the right way to do things.
Trying #audits = Audit.all gives an Uninitialized constant error.
Is there a more graceful way of interacting with models provided by gems?
Maybe something like
include Audited::Adapters::ActiveRecord::Audit
and then you can do
#audits = Audit.all
?
I think that should work... Or better yet:
include Audited
You can access all Audit records with
Audited::Audit.all
I got the result when I typed the
Audited.audit_class
DEPRECATION WARNING: audit_class is deprecated and will be removed
from Rails 5.0 (Audited.audit_class is now always Audited::Audit. This
method will be removed.).
I know this isn't an efficient way to do so, but this is how I do it.
In a Rails console I retrieve a record which I know is audited.
#page = Page.first
I then retrieve that record's first audit.
#audit = #page.audits.first
You can then call #class on #audit
#audit.class
Result:
Audited::Adapters::ActiveRecord::Audit(id: integer, created_at:
datetime, updated_at: datetime, auditable_id: integer, auditable_type:
string, user_id: integer, user_type: string, username: string, action:
string, audited_changes: text, version: integer, comment: string,
full_model: text, remote_address: string, associated_id: integer,
associated_type: string, request_uuid: string)
Audited::Adapters::ActiveRecord::Audit is the class name, which you can then use in your search.
audits = Audited::Adapters::ActiveRecord::Audit.where(:user_id => 8675309)