Find ID of User to add to another field - ruby-on-rails

I am attempting to retrieve the ID of the user from the User table in order to pass the id to the metadata field of the File table. Within the model of the file table I added the relation belongs to :user. When the User uploads a file a record is created in the File table. As a result of this I have also added after_create :update_metadata' to update the metadata field.
Within the 'update_metadata' method I need to find a way to grab the user ID and pass it into the metadata field when a File is uploaded. I assumed user_id = User.find(id) update_attribute(:metadata, user_id) would work but nothing is displayed in the metadata field.
I expected the field to be updated with the User's ID.

If you have added the association in file model as:
belongs_to :user
There must be a user_id present in file object in the database determining which user it belongs to.
So you can access that by file_object.user_id or file_object.user.id
Note: In the after_create callback you can access your file object directly or by using self keyword

Related

How do we add custom fields in Infor syteline

I have a new requirement to add a custom field called document number in the item form which should be editable.
We should use the following steps to create a custom fields.
Creating a user class - The user class definition is the highest level to extend an application database table.
Creating user fields - User fields are generic and can be a part of many classes. If the user changes any property of a user field, all user classes inherit the change.
Associating the user field with a user class - The UET tools look for this association to place the user fields in the form that belongs to the user class.
Linking an application database table with the user class - The association between a table and a class provides the information that UET needs to retrieve, arrange, and display the user fields that belong to a user class. To link the table with the class, define a rule that determines if the record accessed has a valid user class associated with it. If valid data is entered in existing fields to make the rule expression true, the new user field displays.
Impacting the schema - Use the UET Impact Schema form to apply the changes you made in the previous steps to all affected databases.

Rails Access Object Attribute

How would I access a models attribute?
For example, the User model. Let's say I want to access the email portion of the User object.
It does not seem I am able to do
User.email
To access the email of the user.
I was wondering how I would access object attributes in the model and controller.
To get email for all User records, you can do
User.pluck(:email)
If you want to get email a particular user object, you can do
User.find(1).email
Where 1 being the id of the user object in the users table.
I suggest you to read about ActiveRecord query interface to understand better.

Querying embedded documents of one given parent document

I have a mongoid model class "Event" embedding many of class "Participant". Participant has a field "email". Now somewhere in the code I get a reference to one specific Event instance named "#event".
How do I now query the participants of ONLY that instance for an participant with email = XYZ?Not all participants in the collection, just the ones of this given instance #event.
Any ideas?
Thx!
This should work
#event.participants.where(email: 'XYZ') # this will help you query what ever you want
#or
#event.participants.find_by_email 'XYZ' #this will get exactly one record

Rails - How can I save a field value (that uses a record's ID) though a callback?

I want to create a hash that combines the creating user's user_id + the record's ID to make a MD5 hash, but only on record creation. (Reasons are long-winded but this extracts it).
I am trying:
class BlogPost < ActiveRecord::Base
after_create :hash_this
private
def hash_this
self.info_md5_hashed = (id.to_str + creator_user_id).my_MD5_hash_method
end
end
How can I make sure that the info_md5_hashed field actually gets saved to the database?
If I use before_create I would assume that the ID is not yet available? :(
If I use after_create I would assume that the ID is now available
- but do I need to do another save somehow to store the newly calculated info_md5_hashed field value?
Yes, you will have to save the record twice, since ID generation happens in the database. The only way around that is to pre-sequence an ID, but there's almost certainly no point and you should just accept that you will have to save the record twice ;)
Just call the second save from inside your after_create hook- it's ok to do this (i.e. will not be a looping recurrence issue) because the second save won't re-trigger that hook again ;)
n.b. You could always base the hash on something you know to be unique, such as the username or email, if you want to avoid a double-save.

Rails - after_save - How to determine if a record was Created/Update or Deleted

In my rails app I have the following models:
Messages (id, title, content, project_id)
MessageViews (id, message_id, view_status)
Permissions (id, user_id, project_id)
Projects (id, name)
Whenever a user is added to a project which existing messages, I want to create a MessageView record for every message in the project.
Likewise, if a user's project permission is deleted, I want to delete all the user's messageViews associated to that project's messages.
In my permissions model, so far I have:
before_destroy :check_message_views
private
def check_message_views
.....
end
How in check_message_views can I determine if a record is being created/update or deleted.
When created/updated, I want to loop through and check
When deleted, I want to loop through all the project's messages and delete any messageView for that user.
Thanks
Shouldn't your MessageView model belong to a Permission? Then you could set :dependent=>:destroy and everything should work automatically.
Otherwise you can use the destroyed? method.
I don't think there's any sort of equivalent created? method but you could put your code in an after_create callback in your Permissions model. There's an after_destroy callback for that matter as well if you want to be consistent.
Another sort of hacky way around it is to check if the model's created_at time is the same as it's updated_at time. If so then it's been created but hasn't been updated yet.

Resources