Rails 4: Session Expiry?

Rails has “tamper-proof” session cookies. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie. Just make sure you have a long secret. If you want to periodically reset all user sessions change your secret. To answer your question, if you … Read more

Generating an image of a database schema used in a Rails app

Have you tried rake db:schema:dump? Essentially, make sure that your database.yml file is referencing the database you wish to dump, and then run the command. It’ll take all of the tables and indexes in said database and then write it out to schema.rb. Note that you should rename schema.rb once it contains the dump; otherwise, … Read more

Refresh token using Omniauth-oauth2 in Rails application

Omniauth doesn’t offer this functionality out of the box so i used the previous answer and another SO answer to write the code in my model User.rb def refresh_token_if_expired if token_expired? response = RestClient.post “#{ENV[‘DOMAIN’]}oauth2/token”, :grant_type => ‘refresh_token’, :refresh_token => self.refresh_token, :client_id => ENV[‘APP_ID’], :client_secret => ENV[‘APP_SECRET’] refreshhash = JSON.parse(response.body) token_will_change! expiresat_will_change! self.token = refreshhash[‘access_token’] … Read more

How to add a virtual attribute to a model in Ruby on Rails?

Ruby actually lets you create virtual attributes this way, which keeps you from having to manually create getter and setter methods: attr_reader :palindrome #getter attr_writer :palindrome #setter attr_accessor :palindrome #both You can also pass multiple arguments too: attr_accessor :palindrome, :foo, :bar The documentation for it isn’t the greatest.

ActiveRecord appends ‘AND (1=0)’ to end of queries

Rails will generate SQL like AND (1=0) when you query for a column whose value is in an empty array: Child.where(id: []) Intuitively, you’d expect that to generate SQL like SELECT * FROM children WHERE id IN (), but () isn’t actually valid SQL. Since no rows will match that query, though, Rails works around … Read more

Rails form_tag does not send params if disabled: true

Yes, that is the expected behavior. It is probably not mentioned in the Rails documentation because the disabled and readonly control behavior is defined by the W3C spec. See the W3C documentation for Disabled controls, which states “disabled controls cannot be successful”. A “successful” control is defined as being “‘valid’ for submission.” Setting disabled to … Read more

What does class_methods do in concerns?

ActiveSupport::Concern provides syntactic sugar for common Ruby patterns for module mixins. When you are using modules as mixins you can’t just use self to declare class methods like you would from a class: module Foo def self.bar “Hello World” end def instance_method “Hello World” end end class Baz include Foo end irb(main):010:0> Baz.bar NoMethodError: undefined … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)