I have an object that has all the properties from the db from the call lets say:
u = User.find_by_email("[email protected]")
u has first_name
, last_name
, email
, phone
etc.
How can I get all attributes except first_name
and last_name
from the object itself, not by modifying the call to the model?
Array- [value_to_be_removed] will remove the value from the array and return back the rest of the elements. But there is one catch here. You need to reassign to it the original variable containing array for the operation to take effect. Here is my example using my array A.
How to remove unwanted objects in PicWish 1 Upload#N#Add images and click the “upload” button 2 Select#N#Select the picture’s area you want removed 3 Remove#N#The unwanted objects will be removed automatically in seconds More ...
If you want to remove an element of an array at an index, use Array.delete_at (index) command. It will remove the element at the given index. Here is my example using the array A I want to remove the element at index 2 which is 3. So I type in A.delete_at (2) which should remove the element at index 2.
u.attributes.except("first_name", "last_name")
It's probably more future-proof to select the attributes you do want:
u.attributes.slice('email', 'phone')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With