我有4个模型:项目,子项目,子项,用户:
class User < ActiveRecord::Base
has_many :projects
end
class Subproject < ActiveRecord::Base
belongs_to :project
has_many :children, :dependent => :destroy
accepts_nested_attributes_for :children, :allow_destroy => true
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :subprojects, dependent: :destroy
end
class Child < ActiveRecord::Base
end
问题是:
def sendinfoAll
@user = User.all
@children = Children.all
@user.each do |user|
@children.each do |child|
if child.user_id == user.id
UserMailer.welcome_email(user, child.name).deliver_now
end
end
end
end
我想向所有用户发送电子邮件。但是,为什么我不能设置@children = Children.all?
我正在使用Rails 4.02
由于
答案 0 :(得分:2)
您需要在下面一行中使用Child
代替Children
@children = Children.all #wrong
@children = Child.all #correct
因为您需要使用 模型类名称 (即Child
而非Children
)来查询ActiveRecord
。