ãããããªã¬ã¼ã·ã§ã³ã«ã¤ãã¦ã
belongs_to, has_one, has_manyã®åºæ¬
ä»æ¥èªãã ç¯å²ã§ã¯ç¹ã«æ°ãããã¨ã¯ãªãã£ãã
has_and_belongs_to_many
has_and_belongs_to_many(habtm)ã¯å©ç¨ã·ã¼ã³ããããéããã¦ãã¦ãè¦ãã¦ãã¦ãããã¾ã使ããªãããããªãããªã¼ã¨ããæ°ããããçµåãã¼ãã«ãä½ã£ãããããã«æ å ±ãããããä»å ããããªããããªã¬ã¼ã·ã§ã³ã®çæ¹ãæ¤ç´¢ãã¦ããããããçæ¹ãfindãããããªæã¯ãçµåãã¼ãã«ã«æ¤ç´¢æ å ±ã®åãä»å ãã¦ãããæ¹ãã¯ã¨ãªã®çºè¡åæ°ãå°ãªããªãããåæ§ã«has_many :hoge, :through => :hogehgoeãããã¾ã使ãã©ããããªãæ°ãããã
ãã®è©±é¡ã«é¢ãã¦ã¯ããªããããããã¦ããæä¸ãªã®ã§ããããããå°ãæãä¸ãã¦æ¸ãããã¨æãã¾ãã
ã¢ã½ã·ã¨ã¼ã·ã§ã³ã®æ¡å¼µ
ã¢ã½ã·ã¨ã¼ã·ã§ã³ã®æ¡å¼µã£ã¦ããã®ã¯ä¸è¨ã®ããã«ãhas_many*1ã«ãããã¯ã渡ãã¦ããã¦ããã®ä¸ã§ã¡ã½ãããå®ç¾©ãããã¨ã§å®ç¾ã§ãã¾ãã
class User < ActiveRecord::Base has_many :readings has_many :articles, :through => :readings do def rated_at_or_above(rating) find :all, :conditions => ["rating >= ?", rating] end end end
é常ã¯has_manyãæå®ããã ãã§ãä¸è¨ã®ãããªã¡ã½ãããè¤æ°å®ç¾©ãããããã«ãªãã®ã ãã©ããããä¸è¨ã®ã³ã¼ãã§æ¡å¼µã§ããã¨ãã訳ã§ãã
user.articles.build(:name => name)
ã¢ã½ã·ã¨ã¼ã·ã§ã³ã®æ¡å¼µãè¤æ°ã®ã¢ãã«ã§ä½¿ãåãããã¨ãã¯ãã¢ã¸ã¥ã¼ã«ã«ã¡ã½ãããè¨è¿°ãã¦:extendãã©ã¡ã¼ã¿ã§æå®ãã¦ãããã¨ããã
has_many :articles, :extend => RatingFinder
åä¸ãã¼ãã«ç¶æ¿
親ã¨åã®é¢ä¿ãããè¤æ°ã®ã¢ãã«ãä¸ã¤ã®ãã¼ãã«ã§ç®¡çããæ¹æ³ã
ã¾ãããã¼ãã«ã«typeã¨ããååã®åãä½ãã¾ãã
create_table :people do |t| t.column :type, :string # å ¨ã¦ã®ã¢ãã«ã«å ±éã®å t.string :name t.string :email # Customerã¢ãã«å°ç¨å t.decimal :balance # Employeeã¢ãã«å°ç¨å t.integer :reports_to t.integer :dept end
ãã®æ¬¡ã«ãããªæãã§ã¢ãã«ãä½ãã¾ã
class Person < ActiveRecord::Base # ... end class Customer < Person # ... end class Employee < Person # ... end class Manager < Employee # ... end
ããããã¨ãããããã®ã¢ãã«ãDBã«ä¿åããã¨ãã«ãrailsãèªåã§typeåãå ¥åãã¦ããã¾ããããã¦ä¸è¨ã®ããã«findã使ãã¨ãrailsãèªåã§typeåãåç §ãã¦ãtypeåã«å¯¾å¿ããã¯ã©ã¹ã¨ãã¦åå¾ãã¦ããã¾ãã
Manager.create(:name => "manager") manager = Person.find_by_name("manager") p manager.class #=> Manager
ããã¯å©ç¨é »åº¦ãçµæ§ããããã ããã¢ãã«ãã¹ãã¼ãã«ç®¡çã§ããããªã®ã§è¦ãã¦ãããã£ã¨ã
typeåãè¨å®ããæã«æ°ãã¤ãããã¨
åä¸ãã¼ãã«ç¶æ¿ã§ãã¼ãã«ã®ååã«ä½¿ããã¦ãããtypeãã¯ãRubyã®çµã¿è¾¼ã¿ã®ã¡ã½ãã(.classã®å¥å)ãªã®ã§ä¸è¨ã®ããã«ä½¿ãã¨ãããããã¨ã«ãªãã¿ããã
person.type = "Manager"
typeåãè¨å®ããã¨ãã¯ãä¸è¨ã®ããã«ããã·ã¥å½¢å¼ã§è¨å®ãã¦ãããã¨ããã¿ããã§ãã
person[:type] = "Manager"
ããªã¢ã¼ãã£ãã¯ã¢ã½ã·ã¨ã¼ã·ã§ã³
åä¸ãã¼ãã«ç¶æ¿ã¯ãåã¢ãã«ã§å ±éããåãããç¨åº¦ããã°ä½¿ãããã©ãå ±éããåãå°ãªãã¨ãã¯ä½¿ãã«ãããããããã¨ãã«ã¯ããªã¢ã¼ãã£ãã¯ã¢ã½ã·ã¨ã¼ã·ã§ã³ã使ãã¨ãããããã(2008/7/14追è¨ã¨ä¿®æ£ãè¡ãã¾ãã)
ä¾
ãcatalog_entryã«ã¯article, sound, imageã®ããããä¸ã¤ã®ã³ã³ãã³ããå«ã¾ãããã¨ãã£ãå ´åã«ãããããã¾ã¨ããresourceã¨ããä»®ã®æ½è±¡ã¢ãã«ï¼ã®ãããªãã®ï¼ãå®ç¾©ãã¦ããªã¢ã¼ãã£ãã¯ã¢ã½ã·ã¨ã¼ã·ã§ã³ãè¨å®ãã¦ãããã¨ã¹ãã¼ãã«æ¸ãããå ·ä½çã«ã¯ä¸è¨ã®ããã«ããã
ã¾ãããã¼ãã«ã«æ½è±¡ã¢ãã«ã®å¤é¨ãã¼ã¨ã¿ã¤ããè¨å®ããã
create_table :catalog_entries do |t| t.string :name t.datetime :acquired_at t.integer resource_id t.string resource_type end
次ã«ãåã ã®ã¢ãã«ã®ãã¼ãã«å®ç¾©ãããã
create_table :articles do |t| t.column :content, :text end create_table :sounds do |t| t.column :content, :binary end create_table :images do |t| t.column :content, :binary end
ããã¦ãåã ã®ã¢ãã«ã«ã¯ä¸è¨ã®ããã«æ¸ãã¦ãããã
class CatalogEntry < ActiveRecord:Base belongs_to :resource, :polymorphic => true end class Article < ActiveRecord:Base has_one :catalog_entry, :as => :resource end class Sound < ActiveRecord:Base has_one :catalog_entry, :as => :resource end class Image < ActiveRecord:Base has_one :catalog_entry, :as => :resource end
ãã®ããã«ããã¨ãcatalogentry.resouceã«Article, Sound, Imageãè¨å®ããã¨ãã«ãrailsãèªåçã«resource_idã¨resource_typeãå ¥ãã¦ããããåå¾æã«ãrailsãresource_typeãè¦ã¦ãé©åãªã¢ãã«ãªãã¸ã§ã¯ãã¨ãã¦åå¾ãã¦ãããã
a = Article.new(:content => "hoge") c = CatalogEntry.new(:name = "entry", :acquired_at => Time.now) c.resource = a c.save! p c.resource_type #=> "Article"
é¢é£ã¨ã³ããª
ActiveRecord復習 - おもしろWEBサービス開発日記
ActiveRecord復習その2 - おもしろWEBサービス開発日記
åèæ¸ç±
Railsã«ããã¢ã¸ã£ã¤ã«Webã¢ããªã±ã¼ã·ã§ã³éçº ç¬¬2ç
- ä½è : Dave Thomas,David Heinemeier Hansson,Leon Breedt,Mike Clark,Andreas Schwarz,James Duncan Davidson,Justin Gehtland,åç°ä¿®å¾
- åºç社/ã¡ã¼ã«ã¼: ãªã¼ã 社
- çºå£²æ¥: 2007/10/26
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 18人 ã¯ãªãã¯: 300å
- ãã®ååãå«ãããã° (138件) ãè¦ã
*1:has_oneãbelongs_toã§ãã§ããã¿ãã