jbuilder vs rails-api/active_model_serializers for JSON handling in Rails 4

It depends on your preference and needs. If you are working with Ember.js front-end, I’d lean towards active_model_serializers since Ember.js was basically crafted to work well with it (Yehuda Katz is one of the maintainers of active_model_serializers and is on the core team for Ember.js; he gave a talk on the topic a while back). … Read more

Rails 4.1 Mailer Previews and Devise custom emails

For those looking to preview Devise emails without using custom mailers, (but still custom emails) this is what I did: Configure your app for email previewing. Set up the Devise Mailer Preview class a. Rails ~> 4.1 # mailer/previews/devise_mailer_preview.rb class Devise::MailerPreview < ActionMailer::Preview def confirmation_instructions Devise::Mailer.confirmation_instructions(User.first, “faketoken”) end def reset_password_instructions Devise::Mailer.reset_password_instructions(User.first, “faketoken”) end … end … Read more

Change the default value for table column with migration

You have to check which version of ActiveRecord you are using. According to your command rake db:migrate you are still on Ruby on Rails 4.2 or earlier. If you are on ActiveRecord up to 4.2 (change_column_default 4.2.9), there is no from/to option and you can define only the new default option as param. class ChangeDefaultvalueForHideSeasonSelector … Read more

GroupingError: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function

You are not allowed to select reviews.id (selected implicitly through the wildcard *) without adding it to the GROUP BY clause or applying an aggregate function like avg(). The solution is to do one of the following: Remove the wildcard * from your select Add the field reviews.id to your group clause Select reviews.id explicitly … Read more

How to update ruby in windows

I recommend using Chocolatey to manage your ruby installation. It’s a unix style package manager for windows. It’s quite easy to install, you can find instructions on their website. In cmd.exe (run as administrator): @powershell -NoProfile -ExecutionPolicy Bypass -Command “iex ((new-object net.webclient).DownloadString(‘https://chocolatey.org/install.ps1’))” && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin Then you can install ruby: choco install ruby And upgrade … Read more

Rails has_one / has_many, dependent option: What values are available?

Docs are available here Looks like the following options are supported: nil – do nothing (default). :destroy – causes all the associated objects to also be destroyed. :delete_all – causes all the associated objects to be deleted directly from the database (so callbacks will not be executed). :nullify – causes the foreign keys to be … Read more

Test ActiveModel::Serializer classes with Rspec

Assumptions This answer assumes you have the rspec-rails, active_model_serializers and factory_girl_rails gems installed and configured. This answer also assumes you have defined a factory for the Sample resource. Serializer spec For the current version(0.10.0.rc3) of active_model_serializers at the time of writing, ActiveModel::Serializer classes do not receive to_json and are , instead, wrapped in an adapter … Read more

Add Custom Field/Column to Devise with Rails 4

Once your model has its full_name attribute, you will have to configure permitted parameters for the #sign_up and #account_update Devise actions. class ApplicationController < ActionController::Base before_action :configure_devise_permitted_parameters, if: :devise_controller? protected def configure_devise_permitted_parameters registration_params = [:full_name, :email, :password, :password_confirmation] if params[:action] == ‘update’ devise_parameter_sanitizer.for(:account_update) do |u| u.permit(registration_params << :current_password) end elsif params[:action] == ‘create’ devise_parameter_sanitizer.for(:sign_up) do … Read more

Best way to go about sanitizing user input in rails

TL;DR Regarding user input and queries: Make sure to always use the active record query methods (such as .where), and avoid passing parameters using string interpolation; pass them as hash parameter values, or as parameterized statements. Regarding rendering potentially unsafe user-generated html / javascript content: As of Rails 3, html/javascript text is automatically properly escaped … Read more