我对Rails(4,Ruby 2.0)相对较新,目前有一个与我合作的has_many + belongs_to协会。我试图建模的协会是一个“白天”。有几个' Timeslots'每个时段都属于一天。
我的模特看起来像这样:
Day.rb
class Day < ActiveRecord::Base
has_many :timeslots
belongs_to :calendar
end
Timeslot.rb
class Timeslot < ActiveRecord::Base
has_many :events
belongs_to :day
validates_presence_of :start_time
validates_presence_of :end_time
validates_presence_of :day_id
end
Day和Timeslot的架构:
create_table "days", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "timeslots", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.time "start_time"
t.time "end_time"
t.integer "day_id"
end
我看到的问题是我无法将Timeslot对象正确保存到数据库中:
在控制台中:
d = Day.new
=> #<Day id: nil, created_at: nil, updated_at: nil, date: nil>
d.id = 20140322
=> 20140322
d.save
(0.1ms) begin transaction
SQL (22.2ms) INSERT INTO "days" ("created_at", "date", "id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sat, 22 Mar 2014 21:28:27 PDT -07:00], ["id", 20140322], ["updated_at", Sat, 22 Mar 2014 21:28:27 PDT -07:00]]
(6.8ms) commit transaction
=> true
Day.all
Day Load (0.3ms) SELECT "days".* FROM "days"
=> #<ActiveRecord::Relation [#<Day id: 20140322, created_at: nil, updated_at: nil>]>
t = Timeslot.new
=> #<Timeslot id: nil, created_at: nil, updated_at: nil, start_time: nil, end_time: nil, day_id: nil>
t.start_time = Time.now
2014-03-22 21:28:52 -0700
t.end_time = Time.now
2014-03-22 21:28:57 -0700
t.day_id = 20140322
=> 20140322
t.save
(0.1ms) begin transaction
SQL (0.8ms) INSERT INTO "timeslots" ("created_at", "day_id", "end_time", "start_time", "updated_at") VALUES (?, ?, ?, ?, ?) [["created_at", Sat, 22 Mar 2014 21:29:06 PDT -07:00], ["day_id", 20140322], ["end_time", 2014-03-22 21:28:57 -0700], ["start_time", 2014-03-22 21:28:52 -0700], ["updated_at", Sat, 22 Mar 2014 21:29:06 PDT -07:00]]
(7.6ms) commit transaction
=> true
# Here's the problem
Day.all
Day Load (0.2ms) SELECT "days".* FROM "days"
=> #<ActiveRecord::Relation [#<Day id: 20140322, created_at: nil, updated_at: nil>]>
2.0.0-p247 :015 > Timeslot.all
Timeslot Load (0.2ms) SELECT "timeslots".* FROM "timeslots"
=> #<ActiveRecord::Relation [#<Timeslot id: 2, created_at: nil, updated_at: nil, start_time: nil, end_time: nil, day_id: 20140322>]>
a = Day.all.first
Day Load (0.2ms) SELECT "days".* FROM "days" ORDER BY "days"."id" ASC LIMIT 1
=> #<Day id: 20140322, created_at: nil, updated_at: nil>
a.timeslots
Timeslot Load (0.3ms) SELECT "timeslots".* FROM "timeslots" WHERE "timeslots"."day_id" = ? [["day_id", 20140322]]
=&GT; #]&GT;
事实上,Timeslot对象似乎没有被正确创建。我不确定为什么会这样。
答案 0 :(得分:0)
我的文件中有几件我不理解的东西,我认为您应该使用ruby命令行工具来生成文件,以确保不会出错。例如,您的created_At和updated_at不正常,应该自动填充,而不是。
我建议您删除文件并启动这些命令
rails g model day calendar:references
rails g model timeslot start_time:time end_time:time day:references
然后将关联添加到模型
class Day < ActiveRecord::Base
has_many :timeslots
belongs_to :calendar
end
class Timeslot < ActiveRecord::Base
has_many :events
belongs_to :day
validates_presence_of :start_time
validates_presence_of :end_time
# validates_presence_of :day_id #Remove this line for now
end
您可以启动rake db:migrate
并重试(顺便说一下Day.all.first
与Day.first
相同而不是t.day_id = ...
,请尝试t.day = Day.first