Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted attributes from an object in ruby

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?

like image 612
Leon Avatar asked Nov 21 '13 00:11

Leon


People also ask

How do I remove a value from an array in JavaScript?

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?

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 ...

How to remove an element at an index in an array?

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.


2 Answers

u.attributes.except("first_name", "last_name")
like image 174
Zippie Avatar answered Sep 23 '22 13:09

Zippie


It's probably more future-proof to select the attributes you do want:

u.attributes.slice('email', 'phone')
like image 25
AJcodez Avatar answered Sep 21 '22 13:09

AJcodez