Why are my associations not rendered to JSON? - ruby-on-rails

I'm trying to control the JSON rendering of a user object in Rails 3.0.2. Here's the relevant model code:
class User < ActiveRecord::Base
belongs_to :employer
has_and_belongs_to_many :roles
def as_json(options={})
super(options.merge(:include => [:employer, :roles]))
end
end
Here's the JSON representation I get:
{"user":{"employer":{},"roles":[{},{},{}],"email":"user.user#example.com"}}
This user does have three roles, so somehow the :include statement is looking up the association, but the role and employer objects are not getting converted to JSON.
If I had an as_json to either of those models, returning garbage, it still doesn't show up.
Am I doing something wrong, or is this a bug? (It wasn't rendering anything for the associations until I upgraded from Rails 3.0.0, which I learned to do from this question.)

You can try:
to_json(:include => [:employer, :roles]) in place of as_json
http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

My workaround
I'm still not sure why it didn't work, but my workaround is to build the representation I wanted manually.
def serializable_hash(options={})
hash_info = super(options)
hash_info[:employer] = {:name => employer.name}
hash_info[:roles] = roles
hash_info
end
I'm using serializable_hash because that is a more general-purpose method from which Rails can generate JSON or XML or whatever. But the method works the same if you change the name to as_json.

Related

Rails Param Population

When you have a rails resource defined rails seems to automatically create a params entry of attributes for that resource. e.g. if my model Lesson has a subject attribute and I post subject=Maths it automatically creates the param[lesson] = { subject: 'Hello' }. The problem I am having is getting nested attributes to appear within this created lesson array.
I'm using mongoid as my backend and have an association on Lesson called activities. The code looks like this:
class Lesson
include Mongoid::Document
field :subject, type: String
embeds_many :activities, class_name: 'LessonActivity' do
def ordered
#target.sort { |x, y| x.display_order <=> y.display_order }
end
def reorder!
#target.each_with_index { |val, index| val.display_order = index }
end
end
accepts_nested_attributes_for :activities
However I can't work out how I access this activities from within params.require(:lesson).permit :activities
I can access it via params.permit(:activities) but that feels a bit messy
I've done some digging and found out what's going on with this.
It all comes from a rails feature, the Param wrapper, details and api. Which configured for json will automatically pass the attributes of the model into a param of the model name (in this case Lesson).
The attributes of the model that will be populated based on how the model responds to the method attribute_names so this gives two routes to achieve the aims of the question.
1 - Instruct my controller to include activities as part of Lesson parameters, e.g. using this method:
class Api::LessonsController < Api::ApiController
wrap_parameters Lesson, include: Lesson.attribute_names << :activities
2 - Update the attiribute_names method for the model to include :activities
I'm still left with a couple of things to resolve, namely the reason associations aren't part of attribute_names on Mongoid and if overriding it to include attribute names is a bad idea.
Basing on the params you provided for your JSON POST request, you will need the following code to whitelist the params you need:
def activities_params
params.require(:activities).permit(:title, :display_order, :content, :time)
end
The params forwarded by your JSON POST request did not have the :activities hash as a value to the :lesson key so whitelisting the params you need is simple like above.
I think you may have answered you question here:
"how I can make it part of lessons key or why I can't. I'm not passing a lesson parameter "
If I read that correctly, you are not passing the lesson param, just a hash of Activities?
That would explain why you can access
params.permit(:activities)
but not
params.require(:lesson).permit :activities

Overriding find in Mongoid has_many relationship

Using Mongoid 2.4.5 on Rails 3.2.1
I have a Model Book that has_many :pages.
class Book
include Mongoid::Document
has_many :pages
end
class Page
include Mongoid::Document
field :page_number
belongs_to :book
validates_uniqueness_of :page_number, scope: :book
end
I'm using nested resources so that I can get urls like /books/4f450e7a84b93e2b44000001/pages/4f4bba1384b93ea750000003/
What I would like to be able to do is use a url like /books/4f450e7a84b93e2b44000001/pages/3/ to get the third page in that book.
Now the crux of the question:
I want to find the page via a call like Book.find('4f450e7a84b93e2b44000001').pages.find('3') or like Book.find('4f450e7a84b93e2b44000001').pages.find('4f4bba1384b93ea750000003')
I know that I can override the find method in Page with something like
class << self
def find(*args)
where(:page_number => args.first).first || super(args)
end
end
But that doesn't seem to have any effect on the scoped query book.pages.find('3') as it seems the scoped search uses a different find method.
How do I specifically override the find method used by book.pages.find('3')?
Why just do a where criteria on your pages ?
Book.find('4f450e7a84b93e2b44000001').pages.where( :page_number => '3')
You can do a scope to in your Pages
class Page
scope :page_number, lambda{|num| where(:page_number => num) }
end
and use it like :
Book.find('4f450e7a84b93e2b44000001').pages.page_number('3')
Define a to_param method on your Page model that returns the page number. This way all Rails URL helpers use that when building URLs (automatically). Then you can just use something like
#book.pages.where(:page_number => params[:page_id]) # page_id is actually the result of page#to_param
Btw. I don't know how large your books are, but it might make more sense to embed your Pages in the Book from a document-oriented database point of view. The whole relationship business is not native to MongoDB.

Active Record Association with Memcached

Hi for rails model association, i know i can do this:
For example a model class Page.
class Page < ActiveRecord::Base
has_many :parts
end
I can do this:
Page.first.parts.find_by_name('body')
Page.first.parts.class actually returns Array. How can it activate methods for Part model? I found the similar post on How do rails association methods work?
My question is that when i try to use memcache to cache the response for parts methods. Then when i call Page.first.parts.find_by_name('body'), it tells me that the Array doesn't have method find_by_name. How do i solve this problem? I need to have the cache as this is one heavily used methods.
class Page
def parts_with_cache
Rails.cache.fetch("parts_for_page_#{id}", {:expires_in => 1.minutes}) do
parts_without_cache
end
end
alias_method_chain :parts, :cache
end
Since you are getting back an array of Parts objects associated to the Page object unfiltered by part name, just do an Array select method on the result set.
body_parts = Page.first.parts.select{ |part| part.name == 'body' }

How can I add attributes to rendered JSON from relationship without nesting?

Here's the problem: I'm using active record and returning some photo objects. The final consumer of these photo objects is going to be a mobile app.
The response needs to have thumbnail versions returned mobile developer has requested that the JSON coming back look like this..
{
"root_url":'http://place.s3.amazonaws.com/folder/',
"image_300":'image_300.jpg',
"image_600":'image_600.jpg',
"image_vga":'image_VGA.jpg',
"image_full":'image.jpg'
}
and not like this:
{
"root_url":'http://place.s3.amazonaws.com/folder/',
"thumbnails": {
"image_300":'image_300.jpg',
"image_600":'image_600.jpg',
"image_vga":'image_VGA.jpg',
"image_full":'image.jpg'
}
}
so far the easy way is to create columns for each of the thumbnails and wow it works. I don't like getting locked into that though because if we wanted different thumbnails later it would mean adding columns to the db etc. I would much prefer to either just specify the thumbnails in the model class OR have a separate table for thumbnails with one thumb per row of the table.
I've looked at delegate, composed_of, using GROUP_CONCAT in a join.., using :method=> in to_json .. none of these look like options. Is there an easy way to do this?
Basic model example:
class Photo < ActiveRecord::Base
has_many :thumbnails, :as => :thumbs_for #polymorphic
end
class Thumbnail < ActiveRecord::Base
# columns = name, filename
belongs_to :thumb_for, :polymorphic => true
end
So far the result looks like this based on the answer from jesse reiss
def as_json(options)
options ||= {} #even if you provide a default, it ends up as nil
hash = super(options.merge({:include => :thumbnails}))
if thumbs = hash.delete(:thumbnails)
thumbs.each {|t| hash.merge!({t['name']=>t['filename']})}
end
hash
end
You can customize the json serialization of an object pretty simply using the as_json method.
For this, you could do :
def as_json(*args)
hash = super(*args)
hash.merge!(hash.delete("thumbnails"))
end
Or you could do it super manually
def as_json(*args)
hash = super()
thumbnails.each do |thumb|
# build thumbnail json
end
end
You don't have to rely on ActiveRecord's super simplistic json serialization methods.

Rails - serialize another activerecord model

I'm looking to serialize an incomplete/temporary model as an attribute of another model such as:
class User < ActiveRecord::Base
serialize :pending_post
end
Where pending_post is assigned an activerecord model:
...
user.pending_post = Post.new(:title => "Whatever", :message => "whatever")
user.save
But instead of saving the yaml for the new Post model, the pending_post attribute is nil (in the DB and on reload). The serialize works great with other objects, Hashes, Arrays, etc, but comes up nil in this case. This is Rails 2.3.9, but I did a quick test with 3.0.1 and saw the same results. I found this description of the issue from years ago: http://www.ruby-forum.com/topic/101858.
I know I could manually serialize/deserialize the object (which works fine) or serialize just the post.attributes, but I'm curious if anyone knows why this acts as it does? It seems if the new post is saved before being assigned to user.pending_post, then just the ID is saved as the user.pending_post attribute. I'm pretty sure it's intentional and not a bug, but I quite don't understand the reasoning. Is it poor form to serialize an active_record model?
I think you need to serialize/save the attributes, not the post object itself, like so:
user.pending_post = {:title => 'Whatever', :message => 'whatever'}
user.save
Then later you can turn it into a real post:
user.posts.create user.pending_post
And I'd probably take it a step further (as I so often do) with a user method:
def save_post
self.posts.create self.pending_post
end
I hope this helps!

Resources