Я потратил приличное время, пытаясь решить эту проблему. Я думал, что я как-то неправильно установил свой путь к классу, но проблема заключалась в том, что я набрал:
java -cp C:/java/MyClasses C:/java/MyClasses/utilities/myapp/Cool
вместо:
java -cp C:/java/MyClasses utilities/myapp/Cool
Я думал, что значение полностью квалифицированного означает укажите полное имя пути вместо полного имени пакета.
Что-то вроде этого должно работать:
render :json => @programs, :include => {:insurer => {:only => :name}}, :except => [:created_at, :updated_at]
Вы также можете сделать это на уровне модели.
program.rb
def as_json(options={})
super(:except => [:created_at, :updated_at]
:include => {
:company => {:only => [:name]}
}
)
end
end
Теперь в вашем контроллере:
render json: @programs
С помощью jbuilder рассмотрите возможность использования вложенных моделей поддерживаемым образом:
# /views/shops/index.json.jbuilder
json.shops @shops do |shop|
# shop attributes to json
json.id shop.id
json.address shop.address
# Nested products
json.products shop.products do |product|
json.name product.name
json.price product.price
end
end
Попробуйте это. Ref
#`includes` caches all the companies for each program (eager loading)
programs = Program.includes(:company)
#`.as_json` creates a hash containing all programs
#`include` adds a key `company` to each program
#and sets the value as an array of the program's companies
#Note: you can exclude certain fields with `only` or `except`
render json: programs.as_json(include: :company, only: [:name])
Кроме того, не нужно делать @programs
переменной экземпляра, поскольку я предполагаю, что мы не передаем ее в представление.
#includes is used to avoid n+1 query.
# http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
Here is an example for the above example.Lets say you have posts and each post has many comments to it.
@posts = Post.where('id IN [1,2,3,4]').includes(:comments)
respond_to do |format|
format.json {render json: @posts.to_json(:include => [:comments]) }
end
#output data
[
{id:1,name:"post1",comments:{user_id:1,message:"nice"}}
{id:2,name:"post2",comments:{user_id:2,message:"okok"}}
{id:3,name:"post1",comments:{user_id:12,message:"great"}}
{id:4,name:"post1",comments:{user_id:45,message:"good enough"}}
]
я получал ту же ошибку «can not clone Symbol file» при рендеринге json with includes из метода контроллера. избегал его так:
render :json => @list.to_json( :include => [:tasks] )