Rails 5.2
datatables
I am following a mini tutorial, on implementing databables with Rails. The table
class BlogDatatable < AjaxDatatablesRails::ActiveRecord
def view_columns
@view_columns ||= {
id: { source: "Blog.id" },
user: { source: "Blog.user_id"}
title: { source: "Blog.title" },
}
end
def data
records.map do |record|
{
id: record.id,
title: record.title,
DT_RowId: record.id,
}
end
end
def get_raw_records
Blog.all
end
end
What I really want to display for the column user, is the user email, but the user email is in the table Users. How do I implement this with datatables, and stil be able to do sorting, based on the email, not the user_id that's in the blogs table?
Try this method:
Assuming you have the routes set to
resources: blogsYour table in the
index.html.erbwill beAdd
_blog.json.jbuilderin blogsAdd
index.json.jbuilderin blogsThis will create the datatable using json object by taking the @blogs instance variable from your index action
and for your question how to show email in the user column which is in the user table, Assuming you have referenced the user table with blogs table, ie
in your blogs model you should have an association:
now you wil get the user email by
blog.user.email