What is the difference between "update_attributes!" and "update_attributes"? [duplicate] - ruby-on-rails

This question already has answers here:
When do I use save!, create! and update_attributes! in Rails?
(3 answers)
Closed 4 years ago.
In my case, update_attributes is not updating the instance with the latest value but update_attributes! is updating the new value.

update_attributes and update_attributes! are aliases of update and update! https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attributes-21
The difference between update and update! is right there on the docs https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update-21
Updates its receiver just like update but calls save! instead of save, so an exception is raised if the record is invalid and saving will fail.

Related

Is there any opposite of include? method in Ruby? [duplicate]

This question already has answers here:
Ruby: Is there an opposite of include? for Ruby Arrays?
(13 answers)
Closed 4 years ago.
I have a question: Is there any opposite method of .include? I know with unless, but I want to do it with if, can I? I tried with unless:
unless variable.include?("something")
#..
end
I want to do it with if, can I? I know .!include? but it didn't work(i don't really know if this method exists, but I saw it in this forum).
if !variable.include?("something")
in plain Ruby
if variable.exclude?("something")
in Rails

Rails: set params but do not save [duplicate]

This question already has answers here:
Rails update_attributes without save?
(4 answers)
Closed 6 years ago.
What's the call to update a Rails record with new params, say, stored in a hash variable? This:
#user.update(hash)
Will save the record, and since I want to put the call in a callback I don't want to save it, just prepare it to be saved correctly in the callback.
You can use attributes= to set the attributes but not save the record.
#user.attributes = hash
New attributes will be persisted in the database when the object is saved. See http://apidock.com/rails/ActiveRecord/AttributeAssignment/attributes
You can do:
#user.attributes = hash
or
#user.assign_attributes hash
Keep in mind that neither of these will return the object you're working on. If you want that, try Object#tap:
#user.tap { |u| u.assign_attributes hash }

Will my "in?" monkey patch cause issues? [duplicate]

This question already has answers here:
Is there an inverse 'member?' method in ruby?
(5 answers)
Closed 8 years ago.
So a fairly common pattern I've run up against is something like this:
[:offer, :message].include? message.message_type
The inversion of wording there messes me up. So I wrote this little monkey patch for Symbol in specific.
def in? *scope
scope.include? self
end
So now I can do the previous this way:
message.message_type.in? :offer, :message
This works fine and I'm happy with it, but occasionally I need similar functionality for other objects. Model objects in Rails apps being the most common case but strings occasionally, etc.
What kind of issues would I run into if I monkey patched this directly into Object?
Rails (ActiveSupport) already patches Object with this method. Here is the documentation: http://api.rubyonrails.org/classes/Object.html#method-i-in-3F.
Returns true if this object is included in the argument. Argument must be any object which responds to #include?. Usage:
characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true
This will throw an ArgumentError if the argument doesn’t respond to #include?.

What is the underlying code for TableA.create(TableB.all.map(&:attributes))? [duplicate]

This question already has an answer here:
What does map(&:name) do in this Ruby code?
(1 answer)
Closed 8 years ago.
For example, if I use rename method in mongo ruby driver, I can check the code here
What exactly is happening when I am using map(&:attributes)?
I think this means tags.map(&:attributes.to_proc).join(' '), but I am not sure why I am getting "undefined method `each_pair' for Arrayxxxxx" error with this command:
TableA.create(TableB.all.map(&:attributes))
Any insight will be appreciated
map returns an array of whatever is returned by the method call.
so
TableB.all.map(&:attributes)
is basically an array of
[TableB.all[0].attributes,TableB.all[1].attributes,TableB.all[2].attributes,...]
Do you want something like
TableB.all.map(&:attributes).each do |attr|
TableA.create(attr)
end

Rails: Why does find(id) raise an exception in rails? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Model.find(1) gives ActiveRecord error when id 1 does not exist
If there is no user with an id of 1 in the database, trying User.find(1) will raise an exception.
Why is this?
Because that's the way the architects intended find(id) to work, as indicated in the RDoc:
Find by id - This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found for all of the listed ids, then RecordNotFound will be raised.
If you don't want the exception to be raised, use find_by_id, which will return nil if it can't find an object with the specified id. Your example would then be User.find_by_id(1).
Further to runako's explanation, it's actually pretty useful to have the choice of whether an exception is raised or not. I'm working on a blog application and I wanted to add support for viewing the next or previous blog entry. I was able to add two instance methods to my Post model that simply return nil when you try to get the previous post when viewing the first post, or the next post when viewing the last post:
def next
Post.find_by_id(id + 1)
end
def previous
Post.find_by_id(id - 1)
end
This avoids my helper code which conditionally generates the Previous Post/Next Post links from having to handle the RecordNotFound exception, which would be bad because it would be using an exception for control flow.

Resources