Rails: #update_attribute vs #update_attributes

Please refer to update_attribute. On clicking show source you will get following code # File vendor/rails/activerecord/lib/active_record/base.rb, line 2614 2614: def update_attribute(name, value) 2615: send(name.to_s + ‘=’, value) 2616: save(false) 2617: end and now refer update_attributes and look at its code you get # File vendor/rails/activerecord/lib/active_record/base.rb, line 2621 2621: def update_attributes(attributes) 2622: self.attributes = attributes 2623: … Read more

AWS S3: The bucket you are attempting to access must be addressed using the specified endpoint

It seems likely that this bucket was created in a different region, IE not us-west-2. That’s the only time I’ve seen “The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.” US Standard is us-east-1

What’s the best manner of implementing a social activity stream? [closed]

I have created such system and I took this approach: Database table with the following columns: id, userId, type, data, time. userId is the user who generated the activity type is the type of the activity (i.e. Wrote blog post, added photo, commented on user’s photo) data is a serialized object with meta-data for the … Read more

Rails: select unique values from a column

Model.select(:rating) The result of this is a collection of Model objects. Not plain ratings. And from uniq‘s point of view, they are completely different. You can use this: Model.select(:rating).map(&:rating).uniq or this (most efficient): Model.uniq.pluck(:rating) Rails 5+ Model.distinct.pluck(:rating) Update Apparently, as of rails 5.0.0.1, it works only on “top level” queries, like above. Doesn’t work on … Read more

Converting camel case to underscore case in ruby

Rails’ ActiveSupport adds underscore to the String using the following: class String def underscore self.gsub(/::/, “https://stackoverflow.com/”). gsub(/([A-Z]+)([A-Z][a-z])/,’\1_\2′). gsub(/([a-z\d])([A-Z])/,’\1_\2′). tr(“-“, “_”). downcase end end Then you can do fun stuff: “CamelCase”.underscore => “camel_case”

Fully custom validation error message with Rails

Now, the accepted way to set the humanized names and custom error messages is to use locales. # config/locales/en.yml en: activerecord: attributes: user: email: “E-mail address” errors: models: user: attributes: email: blank: “is required” Now the humanized name and the presence validation message for the “email” attribute have been changed. Validation messages can be set … Read more

Rails: Default sort order for a rails model?

default_scope This works for Rails 4+: class Book < ActiveRecord::Base default_scope { order(created_at: :desc) } end For Rails 2.3, 3, you need this instead: default_scope order(‘created_at DESC’) For Rails 2.x: default_scope :order => ‘created_at DESC’ Where created_at is the field you want the default sorting to be done on. Note: ASC is the code to … Read more