ruby - In Rails 4 how do I write code that will retrieve objects instead of active record relations? -


i have code:

sectionheader.where(:doc_id => @doc_id) 

which returns:

 #<activerecord::relation  [#<sectionheader id: 1, section_id: nil, content: "a15+,f15+,a15+,f15+,a15+,f15+,a15+,f15+", created_at: "2014-08-13 18:18:39", updated_at: "2014-08-13 18:18:39", documentname: nil, doc_id: 1, row_number: 3, mergedsectionheader_id: nil>,      #<sectionheader id: 2, section_id: nil, content: "a50+,f50+,a50+,f50+,a50+,f50+,a50+,f50+", created_at: "2014-08-13 18:18:39", updated_at: "2014-08-13 18:18:39", documentname: nil, doc_id: 1, row_number: 12, mergedsectionheader_id: nil>, 

this result set array of activerecord relation objects. how can instead objects of type sectionheader?

there used method seems been deprecated in rails 4 example return array of person objects.

person.find(1, :conditions => "administrator = 1", :order => "created_on desc") 

http://apidock.com/rails/activerecord/base/find/class

as sergio tulentsev commented above.

if want array of sectionheader records relation, can call #to_a

sectionheader.where(:doc_id => @doc_id).to_a 

if wanted eager-load relation, can call #load

sectionheader.where(:doc_id => @doc_id).load 

if wanted skip ar model instantiation completely, call #pluck(col1, col2, ...). return multi-dimentional array representing records.

sectionheader.where(:doc_id => @doc_id).pluck(:id, :section_id, :content) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -