Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell DataTable how to handle associated data tables through ActiveRecord?
    primarykey
    data
    text
    <p>I cannot seem to find the answer to this simple need: how to tell Rails, and hence DataTables, about related data tables? This is based on <a href="http://railscasts.com/episodes/340-datatables?view=asciicast" rel="nofollow">Railscast 340</a>. (Where Ryan Bates warns that the server side processing is more complicated; indeed it is!)</p> <p>I am not too experienced in Rails yet, so am still learning about the various find() methods for telling ROR about related data using Active Record; but the essence of my task is to display in the index.html.erb view file for genotypes, where the tricky bit in the view is:</p> <pre><code> def index respond_to do |format| format.html format.json { render json: GenotypesDatatable.new(view_context) } end end </code></pre> <p>See below for the full class of GenotypesDatatables; this goes in a new class directly under the /app folder.</p> <p>I need to display/edit data from three models: genotypes, gmarkers and gvision. I need to also display data two models related to genotypes, from gsamples and gmarkers. </p> <p>The models are constructed like:</p> <pre><code>class Gmarker &lt; ActiveRecord::Base attr_accessible :marker has_many :genotypes, :dependent =&gt; :delete_all ... class Genotype &lt; ActiveRecord::Base attr_accessible :allele1, :allele2, :run_date belongs_to :gmarkers belongs_to :gsamples ... class Gsample &lt; ActiveRecord::Base belongs_to :gupload has_many :genotypes, :dependent =&gt; :delete_all attr_accessible :box, :labid, :subjectid, :well </code></pre> <p>As can be seen in this error message output from the webserver, the problem lies in not getting the correct data associations: ...</p> <pre><code>CACHE (0.0ms) SELECT COUNT(*) FROM "genotypes" Genotype Load (10.5ms) SELECT "genotypes".* FROM "genotypes" ORDER BY allele1 asc LIMIT 10 OFFSET 0 Completed 500 Internal Server Error in 255ms NameError (undefined local variable or method `f' for #&lt;GenotypesDatatable:0x9833ad4&gt;): app/datatables/genotypes_datatable.rb:24:in `block in data' app/datatables/genotypes_datatable.rb:21:in `data' app/datatables/genotypes_datatable.rb:14:in `as_json' app/controllers/genotypes_controller.rb:7:in `block (2 levels) in index' app/controllers/genotypes_controller.rb:5:in `index' ... </code></pre> <p>The data is supposed to be prepared using server-side processing, but which results in a JSON array that is passed to the jQuery in DataTables. The JSON array gets prepared in the class DataTables:</p> <pre><code>class GenotypesDatatable delegate :params, :h, :link_to, to: :@view def initialize(view) @view = view end def as_json(options = {}) # This is what feeds directly into DataTables { sEcho: params[:sEcho].to_i, iTotalRecords: Genotype.count, iTotalDisplayRecords: genotypes.total_entries, aaData: data } end private def data genotypes.map do |genotype| [ # Note: h is shorthand for html_escape h(Gmarker.find(f.gmarkers_id).marker), h(Gsample.find(f.gsamples_id).labid), h(Gsample.find(f.gsamples_id).subjectid), h(Gsample.find(f.gsamples_id).box), h(Gsample.find(f.gsamples_id).well), h(genotype.allele1), h(genotype.allele2), h(genotype.run_date) ] end end def genotypes @genotypes ||= fetch_genotypes end def fetch_genotypes genotypes = Genotype.order("#{sort_column} #{sort_direction}") genotypes = genotypes.page(page).per_page(per_page) if params[:sSearch].present? genotypes = genotypes.where("labid like :search or category like :search", search: "%#{params[:sSearch]}%") end genotypes end ... </code></pre> <p>Would sure appreciate any pointers here; feel like I'm lost in the jungle without a map or a flashlight!</p> <p>Thanks, Rick Casey </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload