If rec is an active record object and you call rec.attribute_names, it gives you the field names in alphabetical order. If there is a way to get the order that the fields where created into the dabatase on the create_table sql call, I would find that very useful. It seems like there is supposed to be a columns(), and column_names() methods, but I am not seeing them and wondering if they maybe disappeared in rails 3 ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jedrin
2011-Aug-15  22:42 UTC
Re: how to get list of Active record fields in database order ?
I wrote this for now, a terrible hack
  def get_order
    str = self.inspect
    ar = str.split('','')
    ar[0] = ar[0].gsub(/^\S*/,'''')
    ar[ar.length - 1] = ar.last.chop
    puts ''ar''
    p ar
    ar2 = ar.map{|el| el.split(":")[0].strip}
    puts ''ar2''
    p ar2
    del_idxs = []
    ar2.each_with_index do |el,idx|
      if !self.attribute_names.include?(el)
        del_idxs << idx
      end
    end
    puts ''del idx''
    puts "\n\n\n"
    del_idxs.each{|i| ar2.delete_at(i)}
    ar2
  end
On Aug 15, 5:46 pm, Jedrin
<jrubia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> If rec is an active record object and you call rec.attribute_names, it
> gives you the field names in alphabetical order.
>  If there is a way to get the order that the fields where created into
> the dabatase on the create_table sql call, I would find that very
> useful. It seems like there is supposed to be a columns(), and
> column_names() methods, but I am not seeing them and wondering if they
> maybe disappeared in rails 3 ?
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
7stud --
2011-Aug-16  05:12 UTC
Re: how to get list of Active record fields in database order ?
Jedrin wrote in post #1016808:> If rec is an active record object and you call rec.attribute_names, it > gives you the field names in alphabetical order. > If there is a way to get the order that the fields where created into > the dabatase on the create_table sql call, I would find that very > useful.How? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sergio Sergio
2011-Aug-16  14:05 UTC
Re: Re: how to get list of Active record fields in database order ?
column_names is the one you''re looking for, it is a class method, so you should use it like Model.column_names 2011/8/16 7stud -- <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Jedrin wrote in post #1016808: > > If rec is an active record object and you call rec.attribute_names, it > > gives you the field names in alphabetical order. > > If there is a way to get the order that the fields where created into > > the dabatase on the create_table sql call, I would find that very > > useful. > > How? > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.