ArticleDirectory
- Automatic type conversion:
- Record Mapping
- Access Data
- Create
- Find
- Transactions
Rails notes activerecord
It is recommended to restart after modifying the attributes of the model (if the properties change) to avoid strange errors.
Automatic type conversion:
- Int, integer => fixnum
- Decimal, numeric => float
- Clob, blob, text => string
- Interval, date => date
- Float, double => float
- Char, varchar, string => string
- Datetime, time => time
- Boolean => Literal Value
Note:
1. Accuracy after decimal to Ruby float operations may be _ cannot be guaranteed _ (replaced by an integer after amplification) 2. Data represents a special feature of Boolean: Boolean processing modelobj. name1? To access the Boolean of rails (otherwise it is Ruby's built-in low-energy Boolean ).
- Rails: 0, "0", "F", "false", "", nil, falseAllFalse, others are true
- RUBY: nil, false is false,Others are true._ If you want to customize a Boolean, you can write a name1? Method
Record Mapping
Modelclass. Columns. Map {| Col | col. name}
Access Data
Access using object. Property or object [: property] (when it conflicts with Ruby Reserved Words). The latter can also overwrite the default behavior, for example
Class account <activerecord: Base def balance = (value) Raise balancetoolow if value <minimum_level self [: balance] = value endend
Add modelobj1.name1 _ before_type_cast to obtain the original data object (unconverted)
Serialize: last_five can use yaml to serialize any object to the text field, but onlyProgramReadable
Set_primary_key 'another _ column 'allows rails to use another_column as the ID, but the ID attribute is still used for Object-level access (ing to another_column at this time)
The establish_connection method can be used to connect to a database different from the configuration. The parameters ignored in the method are still read from the default value of the configuration.
Object operation Creation
New is in the memory, and the data is hard to order. New do | o |... O. Save end after saving
Create step by step. You can create multiple hash and hash arrays and return the created arrays, such as order = order. Create (Params) (directly create parameters from HTTP)
Find
Parameters supported by find
The join command may not be used much ???
Lineitem. find (: All,: conditions => "PR. title = 'Programming Ruby '",: joins =>" as Li inner join products as PR on Li. product_id = Pr. ID ")
Order. Count (the supported parameter is similar to condition) to obtain the number of records
By default, an exception is not found when searching by ID. However, if you are using a complex find, only the null record (nil or []) objecta is returned. find_by_ SQL can generate freely filled objecta, but if you want to modify it, remember to load ID
Find _ (all) _ by_a_and_ B (A, B) (only support and) can be automatically converted to find (: First (: All),: conditions => 'a =? And B =? ', A, B)
Reload re-read
- Save returns true false
Save! Returns nil or returns exception. If the table contains the lock_version int default 0 field, Optimistic Locking is automatically used.
Delete and delete_all will be deleted directly,
- Destory and destory_all read the object into the memory, call all callback, and then delete the object (no previous steps)
Transactions
Trasaction is implemented in rails, and objects in the memory will be automatically rolled back by calling trasaction (obj1, objec2 ).
Rails's built-in save and delete methods (which may lead to multiple SQL statements) are by default transactional (atomic) without additional transactions.
Rails does not support cross-database transactions (at least currently). The following is a simple simulation solution.
User. Transaction (User) Do account. Transaction (account) Do account. calculate_fees user. date_fees_last_calculated = time. Now user. Save account. Save endend
however, this is just a simulation. If an error occurs during the execution period, you can roll back. However, if an error occurs during user commit, the internal account has been commit and cannot be rolled back.