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 Railscast 340. (Where Ryan Bates warns that the server side processing is more complicated; indeed it is!)
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:
def index
respond_to do |format|
format.html
format.json { render json: GenotypesDatatable.new(view_context) }
end
end
See below for the full class of GenotypesDatatables; this goes in a new class directly under the /app folder.
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.
The models are constructed like:
class Gmarker < ActiveRecord::Base
attr_accessible :marker
has_many :genotypes, :dependent => :delete_all
...
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
...
class Gsample < ActiveRecord::Base
belongs_to :gupload
has_many :genotypes, :dependent => :delete_all
attr_accessible :box, :labid, :subjectid, :well
As can be seen in this error message output from the webserver, the problem lies in not getting the correct data associations:
…
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 #<GenotypesDatatable:0x9833ad4>):
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'
...
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:
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
...
Would sure appreciate any pointers here; feel like I’m lost in the jungle without a map or a flashlight!
Thanks,
Rick Casey
As you know I am doing similar. Rather than try to change your code which would probably confuse the issue, I will post my code as an example. My table neighbours contains associations.
I think the default_scope is irrelevant in this discussion.
and what I think is the relevant code in the NeighboursDatatable class:
end
So I am not sure you need to do the explicit finds. My table displays properly using DataTables. Hope this helps from another ROR newbie.
John