ActiveRecord connection to seekdb sample program
This topic describes how to build an application using ActiveRecord and seekdb to perform basic operations such as creating tables, inserting data, and querying data.
Prerequisites
- You have installed Ruby and RubyGems.
- You have installed seekdb.
Procedure
- Check the versions of Ruby and RubyGems.
- Install the required gems.
- Obtain the seekdb connection information.
- Create and configure a Rails application.
- Run the sample program.
Step 1: Check the versions of Ruby and RubyGems
Open the terminal and run the following command to check the versions of Ruby and RubyGems:
ruby -v
gem -v
Step 2: Install the required gems
-
Install the system dependencies (for example, Ubuntu):
sudo apt-get install ruby-dev mysql-client libmysqlclient-dev -
If you use RVM (Ruby Version Manager):
rvm requirements -
If you use rbenv:
rbenv install -l # View available versions
rbenv install x.x.x # Install a specific version
rbenv global x.x.x # Set the global version -
Use
gemto install ActiveRecord and mysql2:gem install activerecord mysql2
Step 3: Obtain the seekdb connection information
Contact the seekdb deployment personnel or administrator to obtain the database connection string.
mysql -h$host -P$port -u$user_name -p$password -D$database_name
Parameter description:
$host: the IP address for connecting to seekdb.$port: the port for connecting to seekdb.$database_name: the name of the database to access.$user_name: the account for connecting to seekdb.$password: the password for the account.
Step 4: Create and configure a Rails application
-
Create a new Rails application:
rails new activerecord_seekdb -d mysql -
Configure the database connection information:
Add the following configuration to
config/database.yml:development:
adapter: mysql2
encoding: utf8mb4
database: your_database
username: your_username
password: your_password
host: your_host
port: your_port -
Create a sample model:
# app/models/user.rb
class User < ActiveRecord::Base
validates :name, presence: true
end
Step 5: Run the sample program
-
Create a database table:
# db/migrate/20230520_create_users.rb
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end -
Run the migration:
rails db:migrate -
Test in the Rails console:
rails c# Create a record
user = User.create(name: "John Doe")# Query records
users = User.all
Common issues
-
Connection error: If you cannot connect to the database, check the following:
- Whether the database address and port are correct
- Whether the username and password are correct
- Whether the network connection is normal
-
Permission error: If you encounter a permission-related error, make sure the user has sufficient permissions to perform the required operations.
-
SQL syntax error: If the SQL statement has a syntax error, check whether the SQL statement syntax is correct.
Performance optimization suggestions
- Use ActiveRecord's batch operation methods (such as
insert_all) - Use preloading (preload) to avoid N+1 query issues
- Use caching appropriately
- Optimize database indexes