Notes for Ruby on Rails migration: rubyonrails migration
Save schema. rb under version control.Use rake db: scheme: load to replace rake db: migrate to initialize an empty database.Use rake db: test: prepare to update the schema of the test database.
Avoid setting default data in the table. Use the model layer instead.
def amount self[:amount] or 0 end
However, the use of self [: attr_name] is consider
Converting an array to activerecord::relation is a common requirement. For example, in my project, the backup_projects return value of the array form is obtained from the method. In order to sort and page out this collection, I need to convert it to activerecord::relation form. The conversion method is as follows: Project = Project.backup_projects project.map{|i| i.id} @projects = Project.where (: id = = Project) Now @projects is the activerecord::relation type. Original: https://aakashsharmaror
Rails is for Web projects and you have to think about the large amount of traffic, so let's take a look at how rails performs performance testing.
1. To perform performance testing, we first have to imitate a large number of data, we now know that in the test/fixtures/directory in the Yml file to add our test data, when the test run, the data will be loaded into the database. But a piece of two data can al
user model that contains first_name, last_name, nickname, where the nickname field is an array type. The following migration code creates the corresponding table:
Create_table:users do |t|
T.string:first_name
t.string:last_name
t.string:nicknames,: Array => true
end
And for this table, we have a simple model
Class User
We do not use the default value for the field, if we instance a user object, the code is like this.
John = User.create (: first_name => ' J
The essay briefly understands the use of rails ' test and test data, and this time to see how to add a test to a model for deletion and modification.
1. Or using the last written products_test.rb, modify the Test_turth method by the name Test_create and make the contents:
def test_create
assert_kind_of Product, @product
assert_equal 1, @product. ID
assert_equal "pragmatic Ver Sion control ", @product. Title
assert_equal" I to use version control ",
Download the installer online railsinstaller-3.1.0 after automatic installationThe following error occurredC:\users\admin>gem Install Rubygems-updateError:could not find a valid gem ' rubygems-update ' (>= 0), where is:Unable to download data from https://rubygems.org/-Ssl_connect returNed=1 errno=0 State=sslv3 Read server certificate b:certificate verify failed (https://api.rubygems.org/latest_specs.4.8.gz)View Gem version information It feels like the version is too low so I want to update the
I mainly use two plug-ins: rinari and Yari.
Rinari supports fast jump to multiple files in the rails project. In addition, it provides the grep tool, which I found useful. The shortcut keys are easy to remember.
Yari provides an Emacs interface for the RI tool that comes with Ruby, which is compatible with the latest Ruby and easy to operate. You only need to u
Document reprinted by Fire Site Building InstituteTo install the oracle driver using gem in ruby on rails, see the original article:
Reference content is as follows:[1] viewing locally installed plug-insGem list orGem list -- local[2] viewing remote available plug-insGem list -- romote[3] viewing oracle driversGem list -- remote | grep oracleThe result is as follows:Activerecord-oracle_enhanced
At this point, the implementation of this shopping cart is complete. Now we can use Rdoc in ruby to generate documents for our program, just like dotnet, java, and python, rdoc can extract the comments before class and method declaration in our code to generate a document.
For our depot program, run the rake appdoc command in the rails command line,
After that, you can view the generated d
Ruby on Rails (ROR) is equivalent to structs + spring + hibernate, but it is much more convenient, and there is almost no need to write configuration files, the development speed is also much faster (ROR is a 10 times faster than Java, and I don't think this number will be exaggerated ), ror's combination of Ajax technology makes developers feel very easy to develop (more convenient than DWR (Direct Web rem
Label: I/O, AR, SP on C file BSThis problem found during development todayThe per function of kaminari cannot be used in any configuration, and the page size is fixed at 30.At first, I thought it was the gem of ransack. I searched the internet for a long time and found that noFinally, check carefully. It turns out that the gemfile contains will_pagination. After you delete it, everything will return to normal.After you use kaminari in Ruby on
Some Suggestions on Ruby on Rails routing configuration, rubyrails
When you need to add one or more actions to a RESTful Resource (do you really need it ?), Use member and collection routes.
# Differential get 'subscriptions/: id/unsubscribe 'resources: subscriptions # Good resources: subscriptions do get 'unsubscribe', on: member end # differential get 'photos/search' resources: photos # Good resources: ph
Online information There are many, but a lot of pits, some have expired, and some do not conform to the current arrangement of some seemingly also with the OS system version has a relationship, do not copy, according to the actual situation to installMy system version is 10.12.31. Install XcodePlease follow the process installation, here does not elaborate2 HomebrewOfficial website: http://brew.sh/index_zh-cn.html Best FQ under, otherwise have to wait for a long time3.RVMCurl-l Https://get.rvm.i
In the previous section, we demonstrated how to build a list of items, and this time we built a simple shopping cart based on the previous content.
1. First we want to create a table that holds customer shopping information:
Database scripts:
drop table if exists line_items;
CREATE TABLE Line_items (
ID int not NULL auto_increment,
product_id int not null,
quantity int NOT null DEFAU Lt 0,
unit_price decimal (10,2) NOT NULL,
constraint fk_items_product foreign key (product_id) references Pro
The last Test Modeul problem has not been resolved, but the following to continue, this time to test controller.
1. In the test\functional directory, rails has generated a corresponding test file for our controller, and note that Application_controller does not generate test files. Let's take control of the login Logincontroller as an example, open the LOGIN_CONTROLLER_TEST.RB, which reads as follows:
Require File.dirname (__file__) + '/... /test_h
The most frequently run query on a database is to return a conforming result set based on a specified condition, and the query may be to return all the names ' Dave's order, or all the titles on a blog with rails, in many other frameworks and programming languages, you need to create SQL to execute queries, and Active record takes advantage of the dynamic capabilities that the Ruby language contains.
For e
Because in some cases you want to use a nonexistent column, or you want to create a new virtual modelYou can create a new tableless.rb under concerns under models, with the following code:Module tablelessdef self.included (Base)Base.extend (Classmethods)Base.send (: Include,instancemethods)EndModule Instancemethodsdef save (Validate = True)Validate? Valid? : TrueEndEndModule Classmethodsdef column (name, Sql_type = nil, default = nil, NULL = TRUE)Columns sql_type.to_s, NULL)Enddef columns@column
Ruby rails installation process description 1. create a user 2. assign permissions first add a user group: groupadd humou Add User, set the main directory, shell path useradd-g humou-d/home/humou-s/bin/bash-m humoushell path is not set, this user will not be able to log on or directly: useradd-g humou set password passwd humou delete a user: userdel humou corresponding user group will also be deleted to vi
Ruby on Rails internationalization, rubyrails
Language-related settings and strings should not be used in views, models, and controllers. The text should be moved to the language file under config/locales.
When the tags of the ActiveRecord model need to be translated, the scope of activerecord is used:
en: activerecord: models: user: Member attributes: user: name: "Full name"
Then User. model_name.
Explain how to use Cucumber in Ruby on Rails, railscucumber
Use the @ wip label to mark your unfinished scenarios. These scenarios are not considered and are not marked as test failures. When an unfinished scenario is completed and the function test passes, the @ wip tag should be removed to add the scenario to the test suite.Configure your default configuration file to exclude the scenes marked as @ javasc
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.