Add a reference column migration in Rails 4

Rails 4.x When you already have users and uploads tables and wish to add a new relationship between them. All you need to do is: just generate a migration using the following command: rails g migration AddUserToUploads user:references Which will create a migration file as: class AddUserToUploads < ActiveRecord::Migration def change add_reference :uploads, :user, index: … Read more

Rails I18n validation deprecation warning

Important: Make sure your app is not using I18n 0.6.8, it has a bug that prevents the configuration to be set correctly. Short answer In order to silence the warning edit the application.rb file and include the following line inside the Rails::Application body config.i18n.enforce_available_locales = true The possible values are: false: if you want to … Read more

Rails update_attributes without save?

I believe what you are looking for is assign_attributes. It’s basically the same as update_attributes but it doesn’t save the record: class User < ActiveRecord::Base attr_accessible :name attr_accessible :name, :is_admin, :as => :admin end user = User.new user.assign_attributes({ :name => ‘Josh’, :is_admin => true }) # Raises an ActiveModel::MassAssignmentSecurity::Error user.assign_attributes({ :name => ‘Bob’}) user.name # … Read more

Rails 4: how to use $(document).ready() with turbo-links

Here’s what I do… CoffeeScript: ready = -> …your coffeescript goes here… $(document).ready(ready) $(document).on(‘page:load’, ready) last line listens for page load which is what turbo links will trigger. Edit…adding Javascript version (per request): var ready; ready = function() { …your javascript goes here… }; $(document).ready(ready); $(document).on(‘page:load’, ready); Edit 2…For Rails 5 (Turbolinks 5) page:load becomes … Read more

Rails 4: List of available datatypes

Here are all the Rails 4 (ActiveRecord migration) datatypes: :binary :boolean :date :datetime :decimal :float :integer :bigint :primary_key :references :string :text :time :timestamp Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column These are the same as with Rails 3. If you use PostgreSQL, you can also take advantage of these: :hstore :json :jsonb :array :cidr_address :ip_address :mac_address They are stored as … Read more

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