I'm trying to get the column names from several tables having performed a join to combine them.
I tried just doing the following:
User.joins(:user_account, :user_file, :user_education).column_names
but this is only returning the column names from User, as if I had just done the following:
User.column_names
Does anyone know how to get all of the column names from a join? It seems that this isn't often needed because there doesn't seem to be much information about it online. I need it in order to validate user params.
Thanks!
EDIT:
column names in User table:
["id", "status", "accepted_terms", "created_at", "updated_at", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "confirmation_token", "confirmed_at", "confirmation_sent_at", "unconfirmed_email", "provider", "uid"]
column names in User_account table:
["first_name", "last_name", "phone_number", "address"]
I want to be able to get an entire list of column names, i.e.
["id", "status", "accepted_terms", "created_at", "updated_at", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "confirmation_token", "confirmed_at", "confirmation_sent_at", "unconfirmed_email", "provider", "uid", "first_name", "last_name", "phone_number", "address"]
What method do I need to get exactly that list? I have tried
User.joins(:user_account).column_names
but this just gives me the first set of column names, i.e. the ones in just the user table (not the ones in the user account table).
You can simply do this:
User.column_names + UserAccount.column_names
and so on. Rails retrieves column names from ActiveRecord class, not from SQL relation.
UPD. Or more idiomatic
[User, UserAccount, UserFile, UserEducation].flat_map(&:column_names).uniq
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