In our last post, we discussed simple Sequel models. In this post, we'll talk about and show some more complex models using a many_to_one / one_to_many relationship. Here, we're going to revisit the countries we used earlier and add cities to the countries. In this case a country can have many cities, but a city will belong to only a single country. Other examples of this many_to_one / one_to_many
æ¦è¦MySQLã§æååãæ¤ç´¢å¯¾è±¡ã«ããå ´åãä¸è¬çãªããã©ã«ãè¨å®ã§ã¯å¤§æåã»å°æåãåºå¥ããªãããã®åé¡ã«ãã£ããããã£ã¦ãã¾ã£ãã®ã§ã調æ»ã¨å¯¾çãè¡ã£ãã¡ã¢ãæããã¨ã«ããã åºæ¬çãªãã¨ãããªã®ã«ãã®ãããé·ãã®ã§ããã¤ã¸ã§ã¹ãããèªãã§ããã ããã°ããããOKã«ãã¦ãããæ¤è¨¼éç¨ã«èå³ã®ããæ¹ã¯ãã®ç¶ããã©ããã ãã¤ã¸ã§ã¹ãMySQLã¯ããã©ã«ãã§éç¨ããã¨æååã®æ¯è¼ / 並ã³æ¿ãã§å¤§æå / å°æåãåºå¥ããªãã 大æåå°æåãåºå¥ãã¦æ¤ç´¢ããã«ã¯â¦ ãã¼ãã«åä½ã«ã©ã åä½ã¯ã¨ãªåä½â¦ã§ã®å¯¾å¿ãå¯è½ã ãã¼ã¿å®ç¾©ãã¼ãã«åä½ -- CREATE TABLEæã«ãã¼ãã«ã®ããã©ã«ãæåã³ã¼ãã¨ç §åé åºãæå®ãã CREATE TABLE hoge1 ( id INTEGER PRIMARY KEY AUTO_INCREMENT, str VARCHAR(16) ) ENGINE
Open a database require 'rubygems' require 'sequel' DB = Sequel.sqlite 'my_blog.db' DB = Sequel.connect('postgres://user:password@localhost/my_db') DB = Sequel.mysql 'my_db', :user => 'user', :password => 'password', :host => 'localhost' DB = Sequel.ado 'mydb' Open an SQLite memory database Without a filename argument, the sqlite adapter will setup a new sqlite database in RAM. DB = Sequel.sqlite
Migrations This guide is based on guides.rubyonrails.org/migrations.html Overview Migrations make it easy to alter your databaseâs schema in a systematic manner. They make it easier to coordinate with other developers and make sure that all developers are using the same database schema. Migrations are optional, you donât have to use them. You can always just create the necessary database structure
ä¸è¨ã®ãã¼ã¸ãåèã«Sequelã®ä½¿ãæ¹ãåå¼·ããã README.rdoc Sequelã®ç°¡åãªä½¿ãæ¹ ä¾æã«èªåãªãã®è§£èª¬ã追å ããã¨ä¸è¨ã®ããã«ãªã£ãã require 'rubygems' require 'sequel' DB = Sequel.sqlite # ã¡ã¢ãªä¸ã®ãã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããªãã¸ã§ã¯ããDBããä½æ # itemsã¨ãããã¼ãã«ãä½æã DB.create_table :items do primary_key :id # ãã©ã¤ããªãã¼ã¨ãªãIntåã®åãidããä½æ String :name # Stringåã®åãnameããä½æ Float :price # Floatåã®åãpriceããä½æ end items = DB[:items] # ãã¼ãã«ãitemsãã«ã¢ã¯ã»ã¹ãããªãã¸ã§ã¯ããä½æ # ãã¼ãã«ãitemsãã«ã¬ã³ã¼ããæ¿å ¥ i
Sequelã§ãã¢ãã«ã®æ´æ°æã«èªåã§æ´æ°æ¥æãæ¿å ¥ããã«ã¯ãTimestampsãã©ã°ã¤ã³ã使ããããããããcreated_at, updated_atã«ã©ã ãä½æãã¦ããã¦ãã¢ãã«ã®ãªãã§ãã©ã°ã¤ã³ãå¼ã³åºããåã¯datetimeã§ä½æããã 使ãæ¹ã¯ãããªãããã class Item < Sequel::Model(:items) plugin :timestamps, :update_on_create => true end :updated_on_createã¯ãã¢ãã«ã®ä½ææã«ãupdated_atãæ´æ°ãããªãã·ã§ã³ã Rdoc Sequel::Plugins::Timestamps
Rubyã«ããã軽éãªãã¼ã¿ãã¼ã¹ãã¼ã«ãããã¨ãã¦ããªããªããæ軽ãªSequelã§ãããå®éã«ä½¿ãã«ããã£ã¦ããã¤ãæ©ãã ãã¤ã³ãããã£ãã®ã§ãåå¿é²ã¨ãã¦ãã®è§£æ±ºæ³ãã¡ã¢ãã¦ããã¾ãã ãªãããã®è¨äºã¯Ruby 1.9.1p378ã¨Sequel 3.8.0ã®çµã¿åããã対象ã¨ãã¦æ¸ãã¦ãã¾ãã ã¢ãã«ã®å®ç¾©åã«DBæ¥ç¶ãããããªã Sequelã«ãActiveRecordãã¿ã¼ã³ã«åºã¥ããã¢ãã«ã®ä»çµã¿ãç¨æããã¦ãã¾ããããã¢ãã«ãå®ç¾©ããæç¹ã§ãã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ãåå¨ãã¦ããªããã°ãªããªããã¨ããå¶ç´ãããã¾ãã ã¤ã¾ãã require 'sequel' Sequel.connect('sqlite://test.db') class User < Sequel::Model; end p User.all ã¯åãã¾ããã require 'sequel' class User
ãªãªã¼ã¹ãé害æ å ±ãªã©ã®ãµã¼ãã¹ã®ãç¥ãã
ææ°ã®äººæ°ã¨ã³ããªã¼ã®é ä¿¡
å¦çãå®è¡ä¸ã§ã
j次ã®ããã¯ãã¼ã¯
kåã®ããã¯ãã¼ã¯
lãã¨ã§èªã
eã³ã¡ã³ãä¸è¦§ãéã
oãã¼ã¸ãéã
{{#tags}}- {{label}}
{{/tags}}