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