Form has many entries. To serialize form and it's entries I use:
json = #form.to_json( { :only => Form.accessible_attributes.to_a, :include => {:entries => {:only => Entry.accessible_attributes.to_a}}})
Form and entries attributes can be modified or deleted while they are in JSON.
To deserialize I use (but not working) :
#form = #form.from_json(json)
#form.save
How to save entries at once with form?
Had to change entries to entries_attributes:
json = #form.to_json( { :only => Form.accessible_attributes.to_a, :include => {:entries => {:only => Entry.accessible_attributes.to_a}}}).gsub('"entries":[{', '"entries_attributes":[{')
Related
I have an action (using strong parameters) in controller:
def home_task_params
params.require(:home_task).permit(:subject, :description, :data)
end
I want to modify the data before recording to the database. I want to do something similar to this:
def create
#home_task = HomeTask.create(
:subject => home_task_params.subject,
:description => home_task_params.description,
:day => home_task_params.data,
:data => home_task_params.data,
:class_room => current_user.class_room
)
end
How do I implement it?
params is an object that behaves like a hash. Therefore you cannot read values from that object like this params.subject instead, you have to use params[:subject].
Something like this might work:
#home_task = HomeTask.create(
:subject => home_task_params[:subject],
:description => home_task_params[:description],
:day => home_task_params[:data],
:data => home_task_params[:data],
:class_room => current_user.class_room
)
Or you could just merge the additional values to params:
#home_task = HomeTask.create(
home_task_params.merge(
day: home_task_params[:data],
class_room: current_user.class_room
)
)
I want to search a table with multiple conditions in Rails.
I am working on deleting certain package(record) from database but first i need to get userId and packageID .
and here is the code i wrote but it gives error.
#package=Packages.find(:all, :condition => {:id => params[:pid]}, :condition => {:senders_id => cookies[ :user_id ]})
here is the error :
ArgumentError in CreatePackagesController#deletepackage
Unknown key: condition
i just need equivalent code with the right syntax to that one if someone could help.
def deletepackage
#package=Packages.find(:all, :conditions => {:id => params[:pid], :senders_id => cookies[ :user_id ]}
if (#package!=nil && req.receivedByCarrier==false)
#package.destroy
elsif (#package!=nil && req.receivedByCarrier==true)
#package.destroy
end
return;
end
Change your query as below:
#package = Packages.find(:all, :conditions => {:id => params[:pid], :senders_id => cookies[:user_id]})
You are getting the error as Unknown key: condition because :condition is not a valid option in find method.
:condition should be :conditions (Note plural). Also, you should be passing both the conditions as a single key-value pair.
For Rails 4.x
You can simply do it as below
#package = Packages.where(id: params[:pid], senders_id: cookies[:user_id])
This
#package=Packages.find(:all, :condition => {:id => params[:pid]}, :condition => {:senders_id => cookies[ :user_id ]})
should be like this
#package=Packages.find(:all, :conditions => {:id => params[:pid]}, :senders_id => cookies[ :user_id ]})
I did the following query on my model:
output = user.interests.includes([:culture, :sports])
This gives me all the "interests" with all the "culture" and "sports" entries of the user.
I'd like to forward only the id column for "interests", "culture" and "sports" to the client as json.
I tried doing it this way:
output.to_json(:include => [:culture, :sports], :only => ['id'])
When doing it that way it only shows the IDs of the interests but still includes every column of "culture" and "sports". What do I have to add to restrict "culture" and "sports" also to only the IDs?
Thanks in advance!
You can add options for each of the includes by using a separate hash, e.g:
output.to_json(:include => { :culture => { :only => :id }, :sports => { :only => :id } }, :only => ['id'])
I am trying to send a model as json. The model has binary data in one of it's columns. For another model I have used
format.json {self.encode64(#resource_type.data).to_json}
with success, but in that case I only wanted the data column, and not the title etc. What can I do when I want contents from several columns, where only one column's content should be encoded with encode64?
In the following code, I don't know where to put the self.encode64 method.
format.json { render :json => #resource.to_json(:only => [:id, :title, :data])}
How can I do this?
You have a few options here.
You could add a data_base64 method to your model that returned the data in base-64 format and then use the :methods option to to_json in your controller:
#resource.to_json(:only => [ :id, :title ], :methods => :data_base64)
That would give you a data_base64 key in the JSON instead of data but that might not be a problem.
You could also use as_json to get a hash and fix the encoding in the controller:
json = #resource.as_json(:only => [ :id, :title, :data ])
json['resource']['data'] = self.encode64(json['resource']['data'])
render :json => json
You can use as_json in model to override this behaviour like this
def as_json(options={})
{ :name_of_resource => { :created_at => created_at, binary => encode64(self.data) } }
end
You need to specify how he should serialize whole model into json.
Cheers!
render :json => {
"playlist" => playlist_description,
"songs" => #playlist.songs.as_json(:include => {:playlist_songs => {:only => [:id, :position]}})
}
The above code results in 1+N queries to the database, one to load playlist_songs for each song.
The playlist is preloaded in #playlist.
This is so slow, how can I optimize?
My guess: You're not eager-loading the playlist_songs at the moment. You're currently waiting until the as_json call - after which all the songs have been loaded - and then the code has to iterate over every song and fetch the playlist_songs then.
My guess (this is totally untested and may include bugs)
#playlist.songs.all(:include => :playlist_songs).as_json(:include => {:playlist_songs => {:only => [:id, :position]}})
AFAICT, this should first eager load all the songs and the playlist_songs... and then render as json.
I would highly recommend integrating with a JSON builder such as rabl. It will make your life 10x easier moving forward, and is extremely nice to separate the "view" of the JSON representation. I made the switch a couple months ago and haven't looked back.
Within your controller:
#playlist = Playlist.where(:id => params[:id]).includes(:playlist_songs)
Then the rabl template could be something like this:
object #playlist
attribute :description
child :playlist_songs do
attributes :id, :position
end
render :json => {
"playlist" => playlist_description,
"songs" => #playlist.songs.all.as_json(:include => {:playlist_songs => {:only => [:id, :position]}})
}
^ guess