Changeset 6604 for dan

Show
Ignore:
Timestamp:
02/12/08 19:49:53 (10 months ago)
Author:
akio0911
Message:

dan/ruby/akio0911/rails.rb add some lines

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • dan/ruby/akio0911/rails.rb

    r6420 r6604  
    823823=end 
    824824 
     825=begin 
     826Syslog.find(:all, :select=>"*") 
     827Syslog.find(:all, :select=>"id") 
     828# SELECT id FROM syslogs 
     829 
     830Syslog.find(:all, :select=>"id, substring(message,1,20) AS summary") 
     831# SELECT id, substring(message,1,20) AS summary FROM syslogs 
     832=end 
     833 
     834=begin 
     835syslog = Syslog.find(:first) 
     836p syslog.message 
     837 
     838syslog = Syslog.find(:first, :select=>"id, LENGTH(message) AS length") 
     839p syslog.message # NoMethodError 
     840p syslog.length 
     841=end 
     842 
     843=begin 
     844syslog = Syslog.find(:first, :select=>"1, id+priority") 
     845 
     846p syslog.1 #SyntaxError 
     847p syslog.id+priority # NameError 
     848p syslog["1"] # "1" 
     849p syslog["id+priority"] # "3" 
     850=end 
     851 
     852=begin 
     853syslogs = Syslog.find(:all, :select=>"priority, COUNT(*) AS cnt", :group=>"priority", :limit=>5) 
     854# SELECT priority, COUNT(*) AS cnt FROM syslogs GROUP BY priority LIMIT 5 
     855 
     856for syslog in syslogs 
     857  puts "重要度#{syslog.priority}のログは#{syslog.cnt}件です" 
     858end 
     859=end 
     860 
     861=begin 
     862Syslog.find(:all, :joins=>"LEFT JOIN priority_masters USING(priority)") 
     863# SELECT * FROM syslogs LEFT JOIN priority_masters USING(priority) 
     864=end 
     865 
     866=begin 
     867syslog = Syslog.find(:first, :readonly=>true) 
     868syslog.priority = 10 
     869syslog.save # ActiveRecord::ReadOnlyRecord 
     870=end 
     871 
     872=begin 
     873syslog = Syslog.find_by_priority(5) 
     874# SELECT * FROM syslogs WHERE priority = 5 LIMIT 1 
     875 
     876syslog = Syslog.find_by_message("緊急事態") 
     877# SELECT * FROM syslogs WHERE message = '緊急事態' LIMIT 1 
     878=end 
     879 
     880=begin 
     881syslog = Syslog.find_by_priority_and_message(1, '不具合') 
     882# SELECT * FROM syslogs WHERE priority = 1 AND message = '不具合' LIMIT 1 
     883=end 
     884 
     885=begin 
     886Member.find_by_username_and_password(username, password) 
     887=end 
     888 
     889=begin 
     890class Registry < ActiveRecord::Base 
     891end 
     892=end 
     893 
     894=begin 
     895reg = Registry.find_by_name("mailto") 
     896if reg 
     897  reg.update_attribute(:data, "dev@example.co.jp") 
     898else 
     899  Registry.create(:name=>"mailto", :data=>"dev@example.co.jp") 
     900end 
     901=end 
     902 
     903=begin 
     904Registry.find_or_create_by_name("mailto").update_attribute(:data, "dev@example.co.jp") 
     905=end 
     906 
     907=begin 
     908Syslog.find_by_sql("SELECT * FROM syslogs INNER JOIN priority_masters USING(priority)") 
     909=end 
     910 
     911=begin 
     912Syslog.find_by_sql([<<-SQL, :message => '%致命的%', :priority => 10, :days => 3]) 
     913  SELECT * 
     914  FROM syslogs 
     915  LEFT JOIN priority_masters USING(priority) 
     916  WHERE DATEDIFF(created_on, NOW()) < :days) 
     917    AND (priority > :priority OR message LIKE :message) 
     918SQL 
     919=end 
     920 
     921=begin 
     922Syslog.connection.execute("SET NAMES ujis") 
     923 
     924Syslog.connection.execute("VACUUM;") 
     925=end 
     926 
     927=begin 
     928# SELECT COUNT(*) AS cnt FROM syslogs LIMIT 1 
     929syslog = Syslog.find(:first, :select=>"COUNT(*) AS cnt") 
     930p syslog.cnt # "1" 
     931=end 
     932 
     933=begin 
     934Syslog.count 
     935=end 
     936 
     937=begin 
     938# SELECT count(*) AS count_all FROM syslogs WHERE (priority = 1) 
     939Syslog.count("priority = 1") 
     940Syslog.count(["priority = ?", 1]) 
     941 
     942# SELECT count(*) AS count_all FROM syslogs WHERE (priority > 5 AND message = '重要') 
     943Syslog.count(["priority > ? AND message = ?", 5, "重要"]) 
     944=end 
     945 
     946Syslog.count(:distinct=>:priority) 
     947# SELECT COUNT(*) AS count_all FROM (SELECT DISTINCT * FROM syslogs) 
     948 
     949=begin 
     950Syslog.count(:include=>:backtrace, 
     951             :conditions=>"backtraces.appname = 'app1'") 
     952# SELECT count(DISTINCT syslogs.id) as count_all 
     953# FROM syslogs LEFT OUTER JOIN backtraces 
     954# ON backtraces.syslog_id = syslogs.id WHERE (backtraces.appname = 'app1') 
     955=end 
     956 
     957=begin 
     958Syslog.average(:priority) 
     959# SELECT avg(priority) FROM syslogs 
     960 
     961Syslog.maximum(:priority, :conditions=>['message LIKE ?', '%緊急%']) 
     962# SELECT max(priority) FROM syslogs WHERE (message LIKE '%緊急%') 
     963=end 
     964 
     965=begin 
     966# SELECT count(*) AS count_all, priority AS priority FROM syslogs GROUP BY priority 
     967Syslog.count(:group=>:priority) 
     968=end 
     969 
     970=begin 
     971# SELECT priority, COUNT(*) AS cnt FROM syslogs GROUP BY priority 
     972Syslog.find(:all, :select=>"priority, COUNT(*) AS cnt", :group=>:priority) 
     973=end 
     974 
     975=begin 
     976CREATE FUNCTION set_syslogs_updated_on() RETURNS OPAQUE AS ' 
     977  BEGIN 
     978    new.updated_on := ''now''; 
     979    RETURN new; 
     980  END; 
     981' LANGUAGE 'plpgsql'; 
     982 
     983CREATE TRIGGER syslogs_update BEFORE UPDATE ON item FOR EACH ROW 
     984  EXECUTE PROCEDURE set_syslogs_updated_on(); 
     985=end 
     986 
     987=begin 
     988class Syslog < ActiveRecord::Base 
     989  before_save :syslogs_update 
     990 
     991  def syslogs_update(row) 
     992    row[:updated_on] = Time.now 
     993  end 
     994end 
     995=end 
     996 
     997=begin 
     998class Syslog < ActiveRecord::Base 
     999  before_save { |row| row[:updated_on] = Time.now} 
     1000end 
     1001=end 
     1002 
     1003=begin 
     1004class Syslog < ActiveRecord::Base 
     1005end 
     1006=end 
     1007 
     1008=begin 
     1009class Address < ActiveRecord::Base 
     1010  before_save { |row| row[:zipcode].delete!('-')} 
     1011end 
     1012=end 
     1013 
     1014=begin 
     1015class Syslog < ActiveRecord::Base 
     1016  def before_save 
     1017    self[:priority] ||= 1 
     1018    self[:message] ||= "詳細は不明です。でも事件です。" 
     1019  end 
     1020end 
     1021=end 
     1022 
     1023=begin 
     1024class Account < ActiveRecord::Base 
     1025  def before_destroy 
     1026    if self[:login] == 'root' 
     1027      raise "このユーザを削除することはできません" 
     1028    end 
     1029  end 
     1030end 
     1031=end 
     1032 
     1033=begin 
     1034class Account < ActiveRecord::Base 
     1035  def after_destroy 
     1036    Syslog.create(:priority=>3, :message=>"アカウント(#{id})が削除されました") 
     1037  end 
     1038end 
     1039=end 
     1040 
     1041=begin 
     1042class Syslog < ActiveRecord::Base 
     1043  def validate 
     1044    errors.add(:message, "未入力です") if message.blank? 
     1045    errors.add(:priority, "未入力です") if priority == 0 
     1046    errors.add(:priority, "不正な値です") unless priority.between?(1,10) 
     1047  rescue 
     1048    add_to_base("検査中に未知のエラーが発生しました") 
     1049  end 
     1050end 
     1051=end 
     1052 
     1053=begin 
     1054syslog = Syslog.new(:priority=>"ok") 
     1055syslog.save # false 
     1056syslog.errors.empty? # false 
     1057syslog.errors.count # 3 
     1058syslog.errors.on(:priority) # ["未入力です", "不正な値です"] 
     1059syslog.errors.on(:message) # "未入力です" 
     1060=end 
     1061 
     1062=begin 
     1063Syslog.new(:priority=>10).valid? # true 
     1064Syslog.new(:priority=>"x").valid? # false 
     1065=end 
     1066 
     1067=begin 
     1068class Syslog < ActiveRecord::Base 
     1069  def validate 
     1070    errors.add_on_blank([:message, :priority], "未入力です") 
     1071  end 
     1072end 
     1073=end 
     1074 
     1075=begin 
     1076validate_xxx_of ..., :if=>proc{ |row| row[:status] == 1} 
     1077=end 
     1078 
     1079=begin 
     1080class Item < ActiveRecord::Base 
     1081  validates_presence_of :name 
     1082end 
     1083 
     1084Item.new(:name=>"").valid? # false 
     1085Item.new(:name=>nil).valid? # false 
     1086Item.new(:name=>"x").valid? # true 
     1087 
     1088nil.blank? # true 
     1089"".blank? # true 
     10900.blank? # false 
     1091"abc".blank? # false 
     1092=end 
     1093 
     1094=begin 
     1095class Address < ActiveRecord::Base 
     1096  validates_format_of :tel, :with=>/\A[\d-]+\Z/ 
     1097end 
     1098 
     1099Address.new(:tel=>"110").valid? # true 
     1100Address.new(:tel=>"abc").valid? # false 
     1101Address.new(:tel=>"03-").valid? # true 
     1102=end 
     1103 
     1104=begin 
     1105class User < ActiveRecord::Base 
     1106  has_one :account 
     1107  validates_associated :account 
     1108end 
     1109 
     1110class Account < ActiveRecord::Base 
     1111  belong_to :user 
     1112  validates_presence_of :login 
     1113end 
     1114 
     1115user = User.create 
     1116user.create_account(:login=>'guest') 
     1117user.account.valie? # true 
     1118user.valie? # true 
     1119 
     1120user.account[:login] = nil 
     1121user.valid? #false 
     1122user.errors.full_message # ["Account is invalid"] 
     1123=end 
     1124 
     1125=begin 
     1126class Entry < ActiveRecord 
     1127  validates_acceptance_of :accepted 
     1128end 
     1129 
     1130Entry.new.valid? # true 
     1131Entry.new(:accepted=>'1').valid? #true 
     1132Entry.new(:accepted=>'2').valid? # false 
     1133Entry.new(:accepted=>1).valid? # false 
     1134=end 
     1135 
     1136=begin 
     1137class Entry < ActiveRecord 
     1138  validates_acceptance_of :accepted, :accept=>'ok' 
     1139end 
     1140 
     1141Entry.new(:accepted=>'1').valid? # false 
     1142Entry.new(:accepted=>'ok').valid? # true 
     1143=end 
     1144 
     1145=begin 
     1146class Person < ActiveRecord::Base 
     1147  validates_confirmation_of :password 
     1148end 
     1149 
     1150Person.new(:password=>"abc").valid? # true 
     1151Person.new(:password=>"abc", 
     1152           :password_confirmation => "xyz").valid? # false 
     1153Person.new(:password=>"abc", 
     1154           :password_confirmation => "abc").valid? # true 
     1155=end 
     1156 
     1157=begin 
     1158<%= password_field "person", "password" %> 
     1159<%= password_field "person", "password_confirmation" %> 
     1160=end 
     1161 
     1162=begin 
     1163person = Person.new 
     1164person[:password] = "abc" 
     1165person[:password_confirmation] = "xyz" 
     1166person.valid? # true 
     1167person.password # "abc" 
     1168person.password_confirmation # nil 
     1169=end 
     1170 
     1171=begin 
     1172class Person < ActiveRecord::Base 
     1173  validates_each :password do |record, attr, value| 
     1174    score = [/\d/, /[a-z]/, /\W/].inject(0){ |s,r| s+=1 if r===value; s} 
     1175    record.errors.add attr, "強度不足(レベル#{score})" if score < 3 
     1176  end 
     1177end 
     1178 
     1179Person.new(:password=>"0").valid? # false 
     1180Person.new(:password=>"0a").valid? # false 
     1181Person.new(:password=>"0a+").valid? # true 
     1182=end 
     1183 
     1184=begin 
     1185class Person < ActiveRecord::Base 
     1186  validates_exclusion_of :password, :in=>/^\d+$/,  :message=>"数字のみは危険です" 
     1187  validates_exclusion_of :age,      :in=>(20..60), :message=>"雇用対象外です" 
     1188end 
     1189 
     1190class Student < ActiveRecord::Base 
     1191  validates_inclusion_of :school_type, :in=>%w(小 中 高), :message=>"小中高生のみ" 
     1192end 
     1193 
     1194Person.new(:password=>'1120').valid? # false 
     1195Person.new(:age=>13).valid? # false 
     1196Student.new(:school_type=>'大').valid? # false 
     1197=end 
     1198 
     1199=begin 
     1200class Person < ActiveRecord::Base 
     1201  validates_length_of :password, :minimum=>8, :too_short=>'最低%d文字で' 
     1202end 
     1203 
     1204Person.new(:password=>"abc").valid? # false 
     1205=end 
     1206 
     1207=begin 
     1208class Person < ActiveRecord::Base 
     1209  validates_numericality_of :age, :allow_nil=>true, :only_integer=>true 
     1210  validates_numericality_of :weight, :allow_nil=>true 
     1211end 
     1212 
     1213Person.new(:weight=>38).valid? #true 
     1214Person.new(:weight=>38.5).valid? #true 
     1215Person.new(:weight=>'38x').valid? #false 
     1216 
     1217Person.new(:age=>13).valid? # true 
     1218Person.new(:age=>13.5).valid? # false 
     1219Person.new(:age=>'13x').valid? # false 
     1220=end 
     1221 
     1222=begin 
     1223class Account < ActiveRecord::Base 
     1224  validates_uniqueness_of :login, :message=>"重複しています" 
     1225end 
     1226 
     1227Account.create!(:login=>"akio") # [OK] 
     1228Account.create!(:login=>"akio") # RecordInvalid 
     1229=end 
     1230 
     1231=begin 
     1232class Account < ActiveRecord::Base 
     1233  validates_uniqueness_of :login, :scope=>:company_id 
     1234end 
     1235 
     1236Account.create!(:login=>"akio", :company_id=>1) # [OK] 
     1237Account.create!(:login=>"akio", :company_id=>1) # RecordInvalid 
     1238Account.create!(:login=>"akio", :company_id=>2) # [OK] 
     1239=end 
     1240 
     1241=begin 
     1242class Student < ActiveRecord::Base 
     1243  has_one :profile 
     1244end 
     1245 
     1246class Profile < ActiveRecord::Base 
     1247  belongs_to :student 
     1248end 
     1249=end 
     1250 
     1251=begin 
     1252has_one :company, :class_name=>"Office" 
     1253 
     1254has_one :member, :foreign_key=>"jcode" 
     1255 
     1256has_one :office, :foreign_key=>"comcode", :class_name=>"Business::Office" 
     1257=end 
     1258 
     1259=begin 
     1260student = Student.create(:name=>"akio") 
     1261student.profile # nil 
     1262student.profile.nil? # true 
     1263 
     1264profile = Profile.create(:pref=>"tokyo") 
     1265profile.student_id # nil 
     1266student.profile = profile 
     1267profile.student_id # 10 
     1268=end 
     1269 
     1270=begin 
     1271Profile.count # 1 
     1272student = Student.create(:name=>"akio") 
     1273student.create_profile(:pref=>"tokyo") 
     1274Profile.count # 2 
     1275=end 
     1276 
     1277=begin 
     1278profile = Profile.create(:pref=>"tokyo") 
     1279profile.create_student(:name=>"akio") 
     1280profile.student 
     1281profile.student.profile # nil 
     1282profile.save 
     1283profile.student.profile # Profile instance 
     1284=end 
     1285 
     1286=begin 
     1287list = Student.find(:first, :joins=>"LEFT JOIN profiles ON profiles.student_id = students.id") 
     1288list.name # "akio" 
     1289list.pref # "tokyo" 
     1290list.id # 10 
     1291=end 
     1292 
     1293=begin 
     1294SELECT * 
     1295FROM students 
     1296LEFT JOIN profiles ON profiles.student_id = students.id 
     1297LIMIT 1 
     1298=end 
     1299 
     1300=begin 
     1301list = Student.find(:first, :include=>:profile) 
     1302list.id # 1 
     1303list.name # "akio" 
     1304list.profile # Profile instance 
     1305list.profile.id # 10 
     1306list.profile.pref # tokyo 
     1307=end 
     1308 
     1309=begin 
     1310list = Student.find(:first) 
     1311list.id # 1 
     1312list.profile.id # 10 
     1313list.profile.pref # "tokyo" 
     1314=end 
     1315 
     1316=begin 
     1317students = Student.find(:all) 
     1318students.each do |student| 
     1319  puts "#{student.name}の出身は#{student.profile.pref}" 
     1320end 
     1321=end 
     1322 
     1323=begin 
     1324class School < ActiveRecord::Base 
     1325  has_many :students 
     1326end 
     1327 
     1328class Student < ActiveRecord::Base 
     1329  belongs_to :school 
     1330end 
     1331=end 
     1332 
     1333=begin 
     1334school = School.create(:name=>"○×") 
     1335school.students # [] 
     1336school.students.empty? # true 
     1337 
     1338akio = Student.create(:name=>"akio") 
     1339akio.school_id # nil 
     1340school.students << akio 
     1341akio.school_id # 1 
     1342=end 
     1343 
     1344=begin 
     1345school.students.size # 1 
     1346school.students.first.name # "akio" 
     1347Student.create(:name=>"dankogai") 
     1348Student.create(:name=>"amachang") 
     1349school.student_ids = [2,3] 
     1350school.students.size # 2 
     1351school.students.map(&:name) # ["dankogai", "amachang"] 
     1352=end 
     1353 
     1354=begin 
     1355school.students.size # 3 
     1356akio = Student.find(1) # student1 
     1357akio.school_id # 1 
     1358school.students.delete(akio) # [students1] 
     1359school.students.size # 2 
     1360akio.school_id # nil 
     1361akio = Student.find(1) 
     1362=end 
     1363 
     1364=begin 
     1365Student.count("school_id IS NULL") # 1 
     1366school.students.size # 2 
     1367school.students.clear # [] 
     1368school.students.size # 0 
     1369school.students.empty? # true 
     1370Student.count("school_id IS NULL") # 3 
     1371=end 
     1372 
     1373=begin 
     1374school.students 
     1375# SELECT * FROM students WHERE (students.school_id = 1) 
     1376 
     1377school.students.size 
     1378# SELECT COUNT(*) FROM students WHERE (students.school_id = 1) 
     1379 
     1380school.students.find(:all, :conditions=>"name = '...'") 
     1381# SELECT * FROM students WHERE (students.school_id = 1 AND (name = '...')) 
     1382=end 
     1383 
     1384=begin 
     1385School.find(:first, :include=>:students) 
     1386=end 
     1387 
     1388=begin 
     1389class Club < ActiveRecord::Base 
     1390  has_and_belongs_to_many :students 
     1391end 
     1392 
     1393class Student < ActiveRecord::Base 
     1394  has_and_belongs_to_many :clubs 
     1395end 
     1396=end 
     1397 
     1398=begin 
     1399CREATE TABLE clubs_students ("club_id" integer, "student_id" integer) 
     1400=end 
     1401 
     1402=begin 
     1403class Club < ActiveRecord::Base 
     1404  has_and_belongs_to_many :students, :join_table=>"club_and_student", 
     1405    :foreign_key=>"clubcode", :association_foreign_key=>"studentcode" 
     1406end 
     1407 
     1408 
     1409class Student < ActiveRecord::Base 
     1410  has_and_belongs_to_many :clubs, :join_table=>"club_and_student", 
     1411    :foreign_key=>"studentcode", :association_foreign_key=>"clubcode" 
     1412end 
     1413=end 
     1414 
     1415=begin 
     1416dance = Club.create(:name=>"ダンス") 
     1417dance.students # [] 
     1418dance.students << Student.create(:name=>"akio") 
     1419dance.students.create(:name=>"dankogai") 
     1420dance.students.size # 2 
     1421=end 
     1422 
     1423=begin 
     1424Student.create(:name=>"akio") 
     1425Student.count # 3 
     1426music = Club.create(:name=>"音楽") 
     1427music.students = Student.find(:all) 
     1428music.students.size # 3 
     1429=end 
     1430 
     1431=begin 
     1432akio = Student.find_by_name("akio") 
     1433akio.clubs.size # 2 
     1434akio.clubs.map(&:name) # ["ダンス", "音楽"] 
     1435 
     1436dance = Club.find_by_name("ダンス") 
     1437akio.clubs.delete(dance) 
     1438akio.clubs.map(&:name) # ["音楽"] 
     1439=end 
     1440 
     1441=begin 
     1442class Club < ActiveRecord::Base 
     1443  has_many :assigns 
     1444end 
     1445 
     1446class Student < ActiveRecord::Base 
     1447  has_many :assigns 
     1448end 
     1449 
     1450class Assign < ActiveRecord::Base 
     1451  belongs_to :club 
     1452  belongs_to :students 
     1453end 
     1454=end 
     1455 
     1456=begin 
     1457club.students # habtm 
     1458club.assigns.map(&:student).flatten # has_many 
     1459=end 
     1460 
     1461=begin 
     1462class Club < ActiveRecord::Base 
     1463  has_many :assigns 
     1464  has_many :students, :through=>:assigns 
     1465end 
     1466 
     1467class Student < ActiveRecord::Base 
     1468  has_many :assigns 
     1469  has_many :clubs, :through=>:assigns 
     1470end 
     1471 
     1472class Assign < ActiveRecord::Base 
     1473  belongs_to :club 
     1474  belongs_to :student 
     1475end 
     1476=end 
     1477 
     1478=begin 
     1479club.students # has_many :through 
     1480=end 
     1481 
     1482=begin 
     1483club.students << Student.create # [NG] ReadOnlyAssociation 
     1484club.assigns.create(:name => ...) # [OK] 
     1485=end 
     1486 
     1487=begin 
     1488class Student < ActiveRecord::Base 
     1489  has_one :dream, :dependent=>:destroy 
     1490end 
     1491 
     1492class Dream < ActiveRecord::Base 
     1493  belongs_to :student 
     1494end 
     1495=end 
     1496 
     1497=begin 
     1498akio = Student.create(:name=>"akio") 
     1499akio.create_dream(:name=>"pro") 
     1500Student.count # 1 
     1501Dream.count # 1 
     1502 
     1503Student.delete(1) 
     1504Student.count # 0 
     1505Dream.count # 1 
     1506=end 
     1507 
     1508=begin 
     1509akio = Student.create(:name=>"akio") 
     1510akio.create_dream(:name=>"pro") 
     1511Student.count # 1 
     1512Dream.count # 1 
     1513 
     1514Student.destroy(1) 
     1515Student.count # 0 
     1516Dream.count # 0 
     1517=end 
     1518 
     1519=begin 
     1520akio = Student.find(:first) 
     1521akio.destroy 
     1522Student.count # 0 
     1523Dream.count # 0 
     1524=end 
     1525 
     1526=begin 
     1527class Nation < ActiveRecord::Base 
     1528  has_one :king, :dependent=>:destroy 
     1529  has_many :areas, :dependent=>:nullify 
     1530end 
     1531 
     1532class King < ActiveRecord::Base 
     1533  belongs_to :nation 
     1534end 
     1535 
     1536class Area < ActiveRecord::Base 
     1537  belongs_to :nation 
     1538end 
     1539=end 
     1540 
     1541=begin 
     1542shoku = Nation.create!(:name=>"蜀") 
     1543shoku.king = King.create!(:name=>"劉備") 
     1544shoku.areas << Area.create!(:name=>"成都") << Area.create!(:name=>"hogehoge") 
     1545 
     1546Nation.count # 1 
     1547King.count # 1 
     1548Area.count # 2 
     1549 
     1550shoku.destroy 
     1551Nation.count # 0 
     1552King.count # 0 
     1553Area.count # 2 
     1554=end 
     1555 
     1556=begin 
     1557Author.find(:all, :include=>:posts) 
     1558 
     1559Author.find(:all, :include=>[:posts, :categorizations]) 
     1560 
     1561Author.find(:all, :include=>[{ :posts=>:comments}, :categorizations]) 
     1562 
     1563Author.find(:all, :include=>{ :posts=>[:comments, :categorizations]}) 
     1564 
     1565Author.find(:all, :include=>{ :groups=>{ :members=>favorites}}) 
     1566=end 
     1567 
     1568=begin 
     1569class School < ActiveRecord::Base 
     1570  has_many :students 
     1571end 
     1572 
     1573class Student < ActiveRecord::Base 
     1574  has_one :profile, :dependent=>true 
     1575  has_one :dream, :dependent=>true 
     1576  belongs_to :school 
     1577end 
     1578 
     1579class Profile < ActiveRecord::Base 
     1580  belongs_to :student 
     1581end 
     1582 
     1583class Dream < ActiveRecord::Base 
     1584  belongs_to :student 
     1585end 
     1586=end 
     1587 
     1588=begin 
     1589School.find(:all, :include=>[{ :students=>[:profile, :dream]}, :clubs]) 
     1590=end 
     1591 
     1592=begin 
     1593schools.size # 2 
     1594school = schools[0] # <schools(1)> 
     1595school.students.size # 2 
     1596school.students[0].id # 1 
     1597school.students[0].profile 
     1598school.students[0].dream 
     1599school.clubs.size # 3 
     1600=end 
     1601 
     1602=begin 
     1603SELECT schools."id" AS t0_r0, 
     1604       schools."name" AS t0_r1, 
     1605       students."id" AS t1_r0, 
     1606       students."name" AS t1_r1, 
     1607       profiles."id" AS t2_r0, 
     1608       profiles."student_id" AS t2_r1, 
     1609       profiles."birthday" AS t2_r2, 
     1610       profiles."pref" AS t2_r3, 
     1611       dreams."id" AS t3_r0, 
     1612       dreams."name" AS t3_r1, 
     1613       dreams."student_id" AS t3_r2, 
     1614       clubs."id" AS t4_r0, 
     1615       clubs."name" AS t4_r1, 
     1616       clubs."school_id" AS t4_r2 
     1617FROM schools 
     1618LEFT OUTER JOIN students ON students.school_id  = schools.id 
     1619LEFT OUTER JOIN profiles ON profiles.student_id = students.id 
     1620LEFT OUTER JOIN dreams   ON dreams  .student_id = students.id 
     1621LEFT OUTER JOIN clubs    ON clubs   .school_id  = schools.id 
     1622=end 
     1623 
     1624=begin 
     1625require 'open-uri' 
     1626class Response < ActiveRecord::Base 
     1627  serialize :header 
     1628 
     1629  def get 
     1630    open(url) do |f| 
     1631      self.header = f.meta 
     1632      self.body = f.read 
     1633    end 
     1634    save! 
     1635  end 
     1636end 
     1637=end 
     1638 
     1639=begin 
     1640response = Response.new(:url=>"http://www.example.co.jp/item/show/1") 
     1641response.get 
     1642 
     1643response = Response.find(:first) 
     1644response.header.class # Hash 
     1645response.header["content-type"] # "text/xml; charset=UTF-8" 
     1646response.body # "<html>..." 
     1647=end 
     1648 
     1649=begin 
     1650SELECT header FROM responses 
     1651=end 
     1652 
     1653=begin 
     1654class Response < ActiveRecord::Base 
     1655  serialize :header, hash 
     1656end 
     1657 
     1658Response.create!(:header=>{ }) # OK 
     1659Response.create!(:header=>[1,2]) # ActiveRecord::SerializationTypeMismatch 
     1660=end 
     1661 
     1662=begin 
     1663Article.with_scope(:find => { :conditions => "blog_id = 1"}, :create => { :blog_id => 1}) do 
     1664  Article.find(1) # SELECT * from articles WHERE blog_id = 1 AND id = 1 
     1665  a = Article.create(1) 
     1666  a.blog_id # 1 
     1667end 
     1668=end 
     1669 
     1670=begin 
     1671Article.with_scope(:find => { :conditions => "blog_id = 1", :limit => 1}) do 
     1672  Article.with_scope(:find => { :limit => 10}) do 
     1673    Article.find(:all) # SELECT * from articles WHERE blog_id = 1 LIMIT 10 
     1674  end 
     1675  Article.with_scope(:find => { :conditions => "author_id = 3"}) do 
     1676    Article.find(:all) # SELECT * from articles WHERE blog_id = 1 AND author_id = 3 LIMIT 1 
     1677  end 
     1678end 
     1679=end 
     1680 
     1681=begin 
     1682Article.with_scope(:find => { :conditions => "blog_id = 1", :limit => 1}) do 
     1683  Article.with_exclusive_scope(:find => { :limit => 10}) do 
     1684    Article.find(:all) # SELECT * from articles LIMIT 10 
     1685  end 
     1686end 
     1687=end 
     1688 
     1689=begin 
     1690total = Item.count 
     1691count = Item.count("deleted = false") 
     1692item = Item.find(:first, :conditions=>"id = 1 AND deleted = false") 
     1693items = Item.find(:all, :conditions=>"deleted = false", :limit=>10) 
     1694=end 
     1695 
     1696=begin 
     1697total = Item.count # 3 
     1698item.with_scope(:find=>{:conditions=>"deleted = false" }) do 
     1699  count = Item.count # 2 
     1700  item = Item.find(1) # item1 
     1701  items = Item.find(:all, :limit=>10) # [item1, item3] 
     1702end 
     1703=end 
     1704 
     1705=begin 
     1706CREATE VIEW active_items AS SELECT * FROM items WHERE deleted = false; 
     1707=end 
     1708 
     1709=begin 
     1710class ActiveItem < ActiveRecord::Base 
     1711end 
     1712 
     1713total = Item.count # 3 
     1714count = ActiveItem.count # 2 
     1715item = ActiveItem.find(1) # item1 
     1716items = ActiveItem.find(:all, :limit=>10) # [item1, item3] 
     1717=end 
     1718 
     1719=begin 
     1720Item.with_scope(:find=>{ :conditions=>["owner_id = ? AND deleted = false", my_id]}) do 
     1721  count = Item.count 
     1722  item = Item.find(1) 
     1723  items = Item.find(:all, :limit=>10) 
     1724end 
     1725=end 
     1726 
     1727=begin 
     1728Item.with_scope(restriction) do 
     1729  # ... 
     1730end 
     1731