How to update a record? [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the proper way to update a record in Ruby on Rails? Just a single record. For example, I want to modify the name of the title somehow. A code snippet would be awesome!

The basic way to update a record is like that :
#user.name = "new name"
#user.save
this assumes that #user in an instance of a User class having a name field.
you can also do mass_assignment with the update_attributes method
#user.update_attributes name: "new name", email: "new_email#foo.com"
If you want an exception to be raised if the record is invalid you can use the bang method instead, so here it would be save! & update_attributes!
You can also use the update_attribute which has been (or will be soon) renamed to update_column to update a single column while skipping the validation, but you should generally avoid using this method
more doc there
Finally you can use the write_attribute method

entity = Entity.find_by_name("some_name")
entity.title="new title"
entity.save!
or
Entity.find_by_name("some_name").update_attributes(:title => "new title")
for more details, refer to http://guides.rubyonrails.org

Related

Active Record doing nothing [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
please I need a little help... I don't know what I'm doing wrong but I need just a simple select query with Active Record. This looks my code:
Model:
class Kiosk < ApplicationRecord
#kiosk = Kiosk.all
end
Controller:
class KioskController < ApplicationController
def kiosk
#kiosk = Kiosk.all
end
end
HAML:
##kiosk
And it just doing nothing. Even if I change a password of database there isn't any error with connection. rake db:migrade was done a db was created.
Thanks
You may want to look at your logs (eg Rails.root => logs/development.log) or the output in your terminal – are there any error messages? If you log in to your console with eg rails console and run #kiosks = Kiosk.all – what are you seeing? or how about Kiosk.count –– is it showing that there are any kiosks? As mentioned the model looks funny... not sure why you have the #kiosk = Kiosk.all line in there at all....

How do I access the parameters from json API in Rails? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
My rails app is receiving the following from the json. How do I access the name key of attributes and the calendar id in Ruby.
Parameters: {"data"=>{"type"=>"user-events", "attributes"=>{"name"=>"An event", "location"=>"University of Illinois at Urbna-Champaign", "notes"=>"Testing my eventf\n", "all-day"=>false, "recurring"=>false, "sunday"=>false, "monday"=>false, "tuesday"=>false, "wednesday"=>false, "thursday"=>false, "friday"=>false, "saturday"=>false, "start-date"=>"01-16-2018", "stop-date"=>"01-16-2018", "start-time"=>"04:32PM", "stop-time"=>"05:32PM"}, "relationships"=>{"calendar"=>{"data"=>{"type"=>"calendars", "id"=>"685"}}}}}
Do I use the code below to access the name attribute ?
params[:data][:attributes][:name]
Also, do I use the following to access the id for the calendars ?
params[:data][:relationships][:calendar][:data][:id]
Thanks in advance for your help
If your Hash name is Parameters
Parameters['data']['attributes']['name'] # "An event"
Parameters['data']['relationships']['calendar']['data']['id'] # "685"
if it's params, then
params['data']['attributes']['name'] # "An event"
params['data']['relationships']['calendar']['data']['id'] # "685"
will access the attributes.

Making mailer functions more DRY [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Inside a mailer file, I have 11 methods all of which start with the line
#reservation = reservation
Is there a way to make this DRY? I tried the following:
def set_reservation
#reservation = reservation
end
and then
before_action :set_reservation
Unfortunately, this always gave me something along these lines:
AgentReservationMailer#send_reserve_complete_mail: processed outbound mail in 1.7ms
NameError: undefined local variable or method `reservation' for #<AgentReservationMailer:0x007ffc9ae5bb38>
I'm still a very junior level developer, but I would like to try and make things look as professional as I can - is what I am trying to do even possible though?
The reason you're seeing the error is that the mailer does not know of a variable reservation inside the set_reservation method. I'm assuming that the 11 methods you mention, which use the
#reservation = reservation
take reservation as an argument. As it stands, there really is no need to try and reduce duplication.
As a side note, DRY is not a principle you should follow blindly. If you had a couple of lines that were the same in each method, then that would indeed justify an "extract method" refactoring. But replacing that #reservation = reservation assignment with e.g. a method call set_reservation(reservation), you'd still end up repeating one line across all methods.

Use variable in parameter ruby on rails? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to check if param key exists with a variable name and if it exists I want to use value something like params[filenamestring[-1]].
filenamestring is any array generate with split
generally we use params like params[:key] but here i have array and want to use params value with array last element like params[filenamestring[-1]]
You are looking for this:
if params.key?(filenamestring[-1])
This will check if the key exists within the params.
Edit: Something like this would add the param to an array:
my_array << params[filenamestring[-1]] if params.key?(filenamestring[-1])
Or to add it to a string or integer:
my_variable + params[filenamestring[-1]] if params.key?(filenamestring[-1])
If you are doing something else, let me know and I'll update my answer again.

Ruby attribute meta programming [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My user has the attribute:
:step1_local
:step2_local
:step3_local
...
...
:local1
:local2
:local3
I would like to change an attribute value based on another set of attributes on the same model. I would like to do some processing mapping on user, say:
def magic (user)
user.local(1..3) = process(user.step(1..3)_local)
end
The code above of course does not work (example). I am not sure how to do it dynamically without going through each attributes individually. I want to map processing one to another. Any ideas?
You can use Object#public_send and method, like this:
def magic(user)
(1..3).each do |n|
user.public_send("local#{n}=", process(user.read_attribute("step#{n}_local")))
end
end

Resources