Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:59:43+00:00 2026-06-09T03:59:43+00:00

I cannot seem to find the answer to this simple need: how to tell

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:59:45+00:00Added an answer on June 9, 2026 at 3:59 am

    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.

    class Neighbour < ActiveRecord::Base
    
      belongs_to :locality, :class_name => "Locality"
      belongs_to :neighbour, :class_name => "Locality"
      belongs_to :highway,  :class_name => "Highway"
    
      default_scope joins(:locality, :highway).order('localities.name')
    
    end
    

    I think the default_scope is irrelevant in this discussion.

    and what I think is the relevant code in the NeighboursDatatable class:

    def data
      neighbours.map do |neighbour|
        [
         neighbour.locality.name,
         neighbour.neighbour.name,        
         neighbour.highway.name, 
         neighbour.distance,
         neighbour.id
        ]
     end
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple question, but I cannot seem to find a good answer
i cannot seem to find the answer to this. i uploaded code to pastebin
I cannot seem to find the answer to this I am looking for a
I have a relatively simple question that I cannot seem to find the answer
I'm sure this is a very simple fix but I cannot seem to find
This may sound like a simple question, but I just cannot seem to find
I'm sure this has been answered but I cannot seem to find an answer.
I've been searching around for this and cannot seem to find a solid answer.
I cannot seem to find a solid answer on this. I am a novice
I am stuck on this issue and cannot seem to find a way around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.