【发布时间】:2015-01-09 03:20:10
【问题描述】:
我正在使用 Rails 4.1.6 我在创建新记录然后保存它们时遇到问题。以下是模型:
class Function < ActiveRecord::Base
belongs_to :theater
has_many :showtimes, dependent: :destroy
belongs_to :parsed_show
validates :theater, presence: :true
validates :date, presence: :true
end
class Theater < ActiveRecord::Base
has_many :functions, :dependent => :destroy
validates :name, :presence => :true
accepts_nested_attributes_for :functions
end
class Showtime < ActiveRecord::Base
belongs_to :function
validates :time, presence: true
validates :function, presence: true
end
showtime = Showtime.new time: Time.current
theater = Theater.first # read a Theater from the database
function = Function.new theater: theater, date: Date.current
function.showtimes << showtime
function.showtimes.count # => 0
为什么没有将放映时间添加到函数的放映时间?我需要稍后保存该函数以及它的放映时间。
【问题讨论】:
标签: ruby-on-rails validation activerecord append associations