I want to know if there is any array method to check existence of particular column in model. I have combined query results from two tables into an array. I need to check exitence of those tables column name inj that array.
- Please be a bit more elaborate. Your question is not clear enough.Shreyas– Shreyas2010-11-10 10:46:18 +00:00Commented Nov 10, 2010 at 10:46
- Duplicate of How to check if a model has a certain column/attribute? that has better question and answers.Marc-André Lafortune– Marc-André Lafortune2012-07-19 16:49:34 +00:00Commented Jul 19, 2012 at 16:49
Add a comment |
4 Answers
This is how you can check if a model instance is of a particular class, eg obj.is_a?(Person).
To see which columns a certain model has, you do it at class level: obj.class.columns.collect { |c| c.name }.
1 Comment
jordinl
even shorter: obj.class.columns.map(&:name)
You could use this also:
ActiveRecord::Base.connection.column_exists?(:users, :id)