22491

How to include associated objects using gon in Rails/jQuery

Question:

I have a note and it has a certain number of attributes. It also has associated tags. I have been using gon to make this variable available to the javascript files of a page (making @note.attributes available to this page).

I can output the note attributes easily ie if I instantiate:

gon.note = @note.attributes

I can output its content:

alert(gon.note.content)

But, this doesn't put the notes' tags through.. I can't, for example, do :

alert(gon.note.tags)

What if I want to display a note's tags, but I want to bring them through with gon.note rather than make a whole other variable gon.tags. How might I do this (maybe some sort of joins or include)? I want to do this because it's better for, say, if you have multiple notes displaying.

Answer1:

If your @note.tags is a has_many association (not a simple array), <a href="https://github.com/gazay/gon" rel="nofollow">gon</a> doesn't know how to serialize an array of Tag instances.

You can do nothing but introduce another variable:

# controller gon.note_tags = @note.tags.map &:attributes # JS var first_tag_name = gon.note_tags[0].name;

You may want to use <a href="https://github.com/nesquena/rabl" rel="nofollow">Rabl</a> if you need to serialize too many AR objects, since gon supports Rabl.

Answer2:

Rails has a helpful <a href="http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json" rel="nofollow">as_json</a> method that lets you specify your associations, which fields to include, and even methods to include. It returns a ruby hash for single objects or an array for collections, and this will get nicely serialized when gon calls to_json on it.

gon.note = @note.as_json(:include => [:tags]))

Recommend

  • CSS - select element without text inside
  • Show div when page scroll down in jquery
  • Issue in open my application from browser url in android
  • How to get triad census in undirected graph using networkx in python
  • Inheritance and lazy loading in NHibernate
  • php using msaccess
  • Running Applescript from a Cocoa application
  • Starting a javascript prompt after a button is clicked
  • How to Include CSS style when converting svg to png
  • Cycle R,G,B vales as HUE?
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • iOS - open another application that exists on the phone?
  • Bulk loading into PostgreSQL from a remote client
  • Sum children values and save result to parent in n-ary tree in Javascript
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • Refresh JSF component after custom javascript Ajax call
  • Opening tel: links from UIWebView
  • Django self join , How to convert this query to ORM query
  • what do lines starting with double-slash '//' mean in a .npmrc?
  • VB.net Service Programming and using TCP Sockets
  • Videos won't upload
  • Boolean filter using a timestamp value on a dataframe in Python
  • How to find angle formed by the blades of a wind turbine with respect to a horizontal imaginary axis
  • AWS RDS Parameter Group not changing MySQL encoding
  • .Net core Hosted Services guaranteed to complete
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • Getting the type of a “Type” in C# reflection
  • ASP.NET MVC 2 actions for the same route?
  • Year over Year Stats from a Crossfilter Dataset
  • What Exception is thrown on timeout?
  • concise way of flattening multiindex columns
  • Error handeling in antlr 3.0
  • Cloud Code: Creating a Parse.File from URL
  • Android: Unable to detect vertical plane
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Google App Engine backend servlet not responding
  • Update cell query for Excel ADO from Delphi
  • How to get rgb from transparent pixel in js
  • Why does Rails 3 think xE2x80x89 means â x80 x89