I have three table say shop, item and item_type
the shop contains names of shops and item contains items of each shop and item_type contains different types of that items along with the status such as available or not available.
Now i want to render
format.json { render json: {:shop => #shops.as_json(:include => :items)}}
but based on the condition, say items with the item_type_id='1' and status of the item_type_status='available'
Try this:
#shops = Shop.includes(:items).where("items.itemp_type = ?", 'accesories')
format.json { render json: { :shop => #shops.as_json(:include => :items) } }
Edited:
One way you could do this is to create a hash with the objects you want to render, and then pass that to the render method. Like so:
respond_to do |format|
format.json { render :json => {:shops => #shops,
:items => #items }}
end
If the models aren't associated through active record, that's probably your best solution.
If an association does exist, you can pass an :include argument to the render call, like so:
respond_to do |format|
format.json { render :json => #shops.to_json(:include => [:items])}
end
Note that you wouldn't have to retrieve the #items variable in the section above if you take this approach, Rails will automatically load it from the #shops variable.
You can pass method or methods to to_json or as_json methods and include needed records. Example from Rails api:
user.as_json(:methods => :permalink)
# => {"id": 1, "name": "Konata Izumi", "age": 16,
"created_at": "2006/08/01", "awesome": true,
"permalink": "1-konata-izumi"}
Related
I have the shop with has_many association and include items to it so that the items belonging to that shop is received
format.json { render json: {:shop => #shops.as_json(:include => :items)}}
now it gives all the items that belongs to that shop but i want to get items with specific condition, say item_type = "accessories". so how can i do this? please help me.
EDIT
I have put a new question in How to get the value of include with conditions?
You should filter the data with ActiveRecord, and then call the as_json on the filtered data.
You can do something like:
#shops = Shop.includes(:items).where("items.itemp_type = ?", 'accesories')
format.json { render json: { :shop => #shops.as_json(:include => :items) } }
One way you could do this is to create a hash with the objects you want to render, and then pass that to the render method. Like so:
respond_to do |format|
format.json { render :json => {:shops => #shops,
:items => #items }}
end
If the models aren't associated through active record, that's probably your best solution.
If an association does exist, you can pass an :include argument to the render call, like so:
respond_to do |format|
format.json { render :json => #shops.to_json(:include => [:items])}
end
Note that you wouldn't have to retrieve the #items variable in the section above if you take this approach, Rails will automatically load it from the #shops variable.
I have a several models in a heirarchy, 1:many at each level. Each class is associated only with the class above it and the one below it, ie:
L1 course,
L2 unit,
L3 unit layout,
L4 layout fields,
L5 table fields (not in code, but a sibling of layout fields)
I am trying to build a JSON response of the entire hierarchy.
def show
#course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json do
#course = Course.find(params[:id])
#units = #course.units.all
#unit_layouts = UnitLayout.where(:unit_id => #units)
#layout_fields = LayoutField.where(:unit_layout_id => #unit_layouts)
response = {:course => #course, :units => #units, :unit_layouts => #unit_layouts, :layout_fields => #layout_fields}
respond_to do |format|
format.json {render :json => response }
end
end
end
end
The code is bring back the correct values, but the units, unit_layouts and layout_fields are all nested at the same level under course. I would like them to be nested inside their parent.
You need to use to_json with :include to include the associated records.
Here's a stab at it:
#course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json do
render :json => #course.to_json(:include => { :units => { :include => :layouts } })
end
end
It's probably not 100% correct, because you haven't included all the names of your associations, but I'm assuming that Unit has_many Layouts. To include the deeper nesting, add additional nested :includes.
I have the following controller code
respond_with(#employees) do |format|
format.json { render :json => #employees.to_json(:include => :shifts) }
end
What do i have to do if i want to filter the shifts which are included? For example by a date.
I have to be able to set the filter parameters in the controller.
Edit:
I thought about using :method but it creates another variable in the json object. It has to be called "shifts"
format.json { render :json => #employees.includes(:shifts).where("shifts.date > ?", your_date_here).to_json(:include => :shifts) }
[
{
"id":"123",
"name":"House"
},
{
"id":"1456",
"name":"Desperate Housewives"
},
{
"id":"789",
"name":"Dollhouse"
},
{
"id":"10",
"name":"Full House"
}
]
How can I render to produce this JSON format from within Ruby? I have all the data from the DB (#result) and don't know what data structure to use in Ruby that will render to this when I do this:
respond_to do |format|
format.json { render :json => #result}
end
What data structure should #result be and how can I iterate to produce it? Thank you!
If #result is an array of ActiveRecord model instances then render :json => #result will produce something like what you are after, but will include all the attributes of the model (render calls to_json on the object you pass it unless it is a string).
To only include the id and name attributes, you can use the :only parameter of to_json:
respond_to do |format|
format.json { render :json => #result.to_json(:only => [:id, :name] }
end
Alternatively, you can create a array of Hash objects that only contain the required attributes:
respond_to do |format|
format.json { render :json =>
#result.collect {|o| {:id => o.id, :name => o.name} } }
end
Edit: See #dt's comment below. There is an attribute in the model named text that needs to be output as name. This can be done by creating an alias for text in the model:
class Model < ActiveRecord::Base
alias_method :name, :text
and including the name using :methods:
respond_to do |format|
format.json { render :json => #result.to_json(:only => :id, :methods => :name }
end
Alternatively, the array of hashes approach can be used to rename the attribute:
respond_to do |format|
format.json { render :json =>
#result.collect {|o| {:id => o.id, :name => o.text} } }
end
Try using the json gem. It will allow to you do things like
#result.to_json
to convert your structures (say, a Hash) to json format. If you're using Ruby on Rails, this functionality should already be accessible to you, so just create a Hash and call to_json.
To create a JSON object of that particular format, you need a ruby array containing hashes for its elements.
#result = [{:id => "10", :name => "Full House"}, {:id => "789", :name => "blahblah"},...]
Rails will convert the ruby array to json correctly in your render response block:
respond_to do |format|
format.json { render :json => #result}
end
I was using a jQuery plugin (FCBKComplete) that needed the json results with specific key names, specifically 'caption' and 'value', which did not exist in the array I was calling to_json on.
So I did this (hacky, but it works):
render :json => taggings.map { |t| {:caption => t.tag.name, :value => t.tag.name} }.to_json
Where taggings is an array returned by an ActiveRecord find, and that returns json like so:
[{"value":"tag.a","caption":"tag.a"},{"value":"tag.b","caption":"tag.b"}]
This is something I've been stuck on for a while now, and I have to apologize in advance for going into so much detail for such a simple problem. I just want to make it clear what I'm trying to do here.
Scenario
So, there's a model Foo, each Foo can either be red, green, or blue. Having URLs like /reds to list all red objects, and /reds/some-red-object to show a certain object. In that "show" view, there should be next/previous links, that would essentially "find the next RedFoo in alphabetical order, and once at the last RedFoo, the next record should be the first GreenFoo, continuing in alphabetical order, and so on".
I've tried implementing this in a couple of ways and mostly ended up at a roadblock somewhere. I did get it working for the most part with single table inheritance though, having something like this:
class Foo < ActiveRecord::Base
class RedFoo < Foo
class GreenFoo < Foo
class BlueFoo < Foo
Each subclass's models and controllers are identical, just replace the model names. So the controllers look something like:
class RedFoosController < ApplicationController
def index
#foos = RedFoo.find(:all, :order => "title ASC")
respond_to do |format|
format.html { render :template => 'foos/index'}
format.xml { render :xml => #foos }
end
end
def show
#foo = RedFoo.find(params[:id])
respond_to do |format|
format.html { render :template => 'foos/show'}
format.xml { render :xml => #foo }
end
end
def new
#foo = RedFoo.new
respond_to do |format|
format.html { render :template => 'foos/new'}
format.xml { render :xml => #foo }
end
end
def edit
#foo = RedFoo.find(params[:id])
respond_to do |format|
format.html { render :template => 'foos/edit'}
end
end
def create
#foo = RedFoo.new(params[:foo])
respond_to do |format|
if #foo.save
flash[:notice] = 'Foo was successfully created.'
format.html { redirect_to(#foo) }
format.xml { render :xml => #foo, :status => :created, :location => #foo }
else
format.html { render :action => "new" }
format.xml { render :xml => #foo.errors, :status => :unprocessable_entity }
end
end
end
def update
#foo = RedFoo.find(params[:id])
respond_to do |format|
if #foo.update_attributes(params[:foo])
flash[:notice] = 'Foo was successfully updated.'
format.html { redirect_to(#foo) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #foo.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#foo = RedFoo.find(params[:id])
#foo.destroy
respond_to do |format|
format.html { redirect_to(foos_url) }
format.xml { head :ok }
end
end
end
The models only contain methods for next/previous, which work fine, surprisingly.
class RedFoo < Foo
def next
if self == RedFoo.find(:all, :order => "title ASC").last
GreenFoo.find(:all, :order => "title ASC").first
else
RedFoo.find(:first, :conditions => ["title > ?", self.title], :order => "title ASC")
end
end
def previous
if self == RedFoo.find(:all, :order => "title ASC").first
BlueFoo.find(:all, :order => "title ASC").last
else
RedFoo.find(:first, :conditions => ["title < ?", self.title], :order => "title DESC")
end
end
end
Problem
For whatever reason when I try to create and edit records, none of the attributes get saved in the database. It simply adds a new record with completely empty columns, regardless of what's filled in the form. No errors get returned in the script/server output or in the log files. From the script/console however, everything works perfectly fine. I can create new records and update their attributes no problem.
It's also quite a bad code smell that I have a lot of code duplication in my controllers/models (they're using the same views as the base model, so that's fine though). But I think that's unavoidable here unless I use some meta-goodness.
Any advice or suggestions about tackling this record saving issue would be great, but the reason I posted my setup in detail is because I have a feeling I'm probably going about this whole thing the wrong way. So, I'm open to other approaches if you know of something more practical than using STI. Thanks.
Update
The parameters hash looks about right:
{"commit"=>"Create", "authenticity_token"=>"+aOA6bBSrZP2B6jsDMnKTU+DIAIkhc8fqoSicVxRJls=", "red_foo"=>{"title"=>"Hello world!"}}
But #foo.inspect returns the following RedFoo object (all nil, except for type):
#<RedFoo id: nil, title: nil, type: "RedFoo", created_at: nil, updated_at: nil>
Problem is the params
:red_foo
is the name of the params in the view, whereas you use
params[:foo]
in the controller, I think the best way would be to be use :foo, in the view by using text_field_tag rather than any (what i assume can be) form builders text_field.
You can get out of the controller smell by using a module to do the basic crud stuff, since i assume most of the new/create/edit/update/destroy stuff is the same
OR
you could map all the routes to a foo controller and use some sort of parameter either passed in from the route, or through URI analysis to get the red/green/blue foo
Please take a look at the section called "Single table inheritance" on this page and let us know if it solves your problem.
Must admit, the way I go about STI is to use set_table_name inside a model.
e.g.
class RedFoo < AR::Base
set_table_name "foos"
include FooModule
extend FooClassModule # for self methods
def next; ...; end
end
But anyway, for this situation, what does your logger say when you do a #foo.inspect just before a save, and also what is the SQL that is ran on insert/update?
Right, so #foo.inspect gives you "nil" in the log?
What I mean (if I wasn't clear enough) was:
def create
#foo = RedFoo.new(params[:foo])
logger.error "******************* foo: #{#foo.inspect} **************"
respond_to do |format|
if #foo.save
...
if you do that and tail -f your log you can easily find out what is happening to foo and compare that to the incoming params hash
Infact, that would also be some useful information to have, what is the params hash?