each migration as being a new 'version' of the database.
1More
Real Artists Ship - Native Issue Tracking - 1 views
17More
Active Record Migrations - Ruby on Rails Guides - 0 views
-
A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries
-
Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.
- ...14 more annotations...
-
A primary key column called id will also be added implicitly, as it's the default primary key for all Active Record models
-
On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction
-
If the database does not support this then when a migration fails the parts of it that succeeded will not be rolled back. You will have to rollback the changes that were made by hand.
-
If your adapter supports DDL transactions you can use disable_ddl_transaction! to disable them for a single migration
-
a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration.
45More
Active Record Callbacks - Ruby on Rails Guides - 0 views
-
Active Record provides hooks into this object life cycle so that you can control your application and its data.
- ...42 more annotations...
-
after_initialize callback will be called whenever an Active Record object is instantiated, either by directly using new or when a record is loaded from the database
-
should be used with caution, however, because important business rules and application logic may be kept in callbacks.
-
As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate
-
When using the :if option, the callback won't be executed if the predicate method returns false; when using the :unless option, the callback won't be executed if the predicate method returns true.
-
needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method.
-
Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.
-
very similar to the after_save callback except that they don't execute until after database changes have either been committed or rolled back
-
If anything raises an exception after the after_destroy callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state
-
-
The after_commit and after_rollback callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block.
120More
Active Record Validations - Ruby on Rails Guides - 0 views
-
Model-level validations are the best way to ensure that only valid data is saved into your database.
- ...117 more annotations...
-
Database constraints and/or stored procedures make the validation mechanisms database-dependent and can make testing and maintenance more difficult
-
combined with other techniques, client-side validation can be a convenient way to provide users with immediate feedback
-
Active Record uses the new_record? instance method to determine whether an object is already in the database or not.
-
Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database
-
After Active Record has performed validations, any errors found can be accessed through the errors.messages instance method
-
To verify whether or not a particular attribute of an object is valid, you can use errors[:attribute]. I
-
All of them accept the :on and :message options, which define when the validation should be run and what message should be added to the errors collection if it fails, respectively.
-
'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
-
use this helper when your model has associations with other models and they also need to be validated
-
-
This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with "_confirmation" appended.
-
The exclusion helper has an option :in that receives the set of values that will not be accepted for the validated attributes.
-
validates the attributes' values by testing whether they match a given regular expression, which is specified using the :with option.
-
:wrong_length, :too_long, and :too_short options and %{count} as a placeholder for the number corresponding to the length constraint being used.
-
-
validate associated records whose presence is required, you must specify the :inverse_of option for the association
-
an association is present, you'll need to test whether the associated object itself is present, and not the foreign key used to map the association
-
validate the absence of a boolean field you should use validates :field_name, exclusion: { in: [true, false] }.
-
a :scope option that you can use to specify other attributes that are used to limit the uniqueness check
-
a :case_sensitive option that you can use to define whether the uniqueness constraint will be case sensitive or not.
-
To implement the validate method, you must have a record parameter defined, which is the record to be validated.
-
the validator will be initialized only once for the whole application life cycle, and not on each validation run, so be careful about using instance variables inside it.
-
The block receives the record, the attribute's name and the attribute's value. You can do anything you like to check for valid data within the block
-
the :message option lets you specify the message that will be added to the errors collection when validation fails
-
You can do that by using the :if and :unless options, which can take a symbol, a string, a Proc or an Array.
-
:expiration_date_cannot_be_in_the_past, :discount_cannot_be_greater_than_total_value
-
errors[:base] is an array, you can simply add a string to it and it will be used as an error message.
-
use this method when you want to say that the object is invalid, no matter the values of its attributes.
-
calling errors.clear upon an invalid object won't actually make it valid: the errors collection will now be empty, but the next time you call valid? or any method that tries to save this object to the database, the validations will run again.
118More
Active Record Migrations - Ruby on Rails Guides - 0 views
-
-
- ...114 more annotations...
-
Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.
-
A primary key column called id will also be added implicitly, as it's the default primary key for all Active Record models
-
On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction
-
change_table is also reversible, as long as the block does not call change, change_default or remove.
-
add_column add_foreign_key add_index add_reference add_timestamps change_column_default (must supply a :from and :to option) change_column_null create_join_table create_table disable_extension drop_join_table drop_table (must supply a block) enable_extension remove_column (must supply a type) remove_foreign_key (must supply a second table) remove_index remove_reference remove_timestamps rename_column rename_index rename_table
-
By default, the name of the join table comes from the union of the first two arguments provided to create_join_table
-
-
If the column names can not be derived from the table names, you can use the :column and :primary_key options.
-
-
If your migration is irreversible, you should raise ActiveRecord::IrreversibleMigration from your down method.
-
the db:migrate task also invokes the db:schema:dump task, which will update your db/schema.rb file to match the structure of your database.
-
-
db:reset task will drop the database and set it up again. This is functionally equivalent to rails db:drop db:setup.
-
If you have already run the migration, then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rails db:migrate.
-
must rollback the migration (for example with bin/rails db:rollback), edit your migration and then run rails db:migrate to run the corrected version.
-
revert method can be helpful when writing a new migration to undo previous migrations in whole or in part
-
Schema files are also useful if you want a quick look at what attributes an Active Record object has
-
annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality.
-
db/schema.rb cannot express database specific items such as triggers, stored procedures or check constraints
-
you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database
-
-
set in config/application.rb by the config.active_record.schema_format setting, which may be either :sql or :ruby.
-
Validations such as validates :foreign_key, uniqueness: true are one way in which models can enforce data integrity
-
The :dependent option on associations allows models to automatically destroy child objects when the parent is destroyed.
-
To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy.
36More
shared by 張 旭 on 22 Jan 14
- No Cached
Active Record Basics - Ruby on Rails Guides - 0 views
guides.rubyonrails.org/active_record_basics.html
ruby rails programming development framework model database object

-
Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database
- ...33 more annotations...
-
Object-Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system
-
The idea is that if you configure your applications in the very same way most of the times then this should be the default way.
-
Primary keys - By default, Active Record will use an integer column named id as the table's primary key
-
Validation is a very important issue to consider when persisting to database, so the methods create, save and update take it into account when running: they return false when validation fails and they didn't actually perform any operation on database.
-
-
1More
Unique gradient generator - 0 views
-
"This tool helps you to generate beautiful blurry background images that you can use in any project. It doesn't use CSS3 gradients, but a rather unique approach. It takes a stock image, extracts a very small area (sample area) and scales it up to 100%. The browser's image smoothing algorithm takes care of the rest. You can then use the image as an inline, base64 encoded image in any HTML element's background, just click Generate CSS button at the bottom of the app. Select source images from the gallery or use yours, the possibilities are endless."
1More
How to Safely Store Your Users' Passwords in 2016 - Paragon Initiative Enterprises Blog - 0 views
1More
interact.js - JavaScript drag and drop, resizing and gestures with inertia and snapping - 0 views
View AllMost Active Members
View AllTop 10 Tags
- 151system
- 133programming
- 102docker
- 101rails
- 89development
- 83devops
- 81kubernetes
- 80javascript
- 77database
- 71ruby
- 68linux
- 64web
- 61server
- 58networking
- 52security
- 49python
- 42mysql
- 42php
- 40framework
- 35performance