Hi,
我需要为我的应用程序中的所有用户(居民)生成账单,bill_控制器的create方法如下所示:
def create
resident = Resident.find_by(hostel:current_admin_user.hostel) # current_admin_user action is provided by Activeadmin to access current activeadmin user details
@bill=resident.bills.create(bill_params)
if @bill.save
flash[:info] = "Bills Generated successfully"
redirect_to new_bill_path
else
flash[:danger] = "Bills Not generated, Please try again!"
redirect_to new_bill_path
end
end主动管理用户可以生成账单,并且账单将只为与管理用户有相同宿舍的居民生成!并为所有有特定宿舍的居民制定法案。查看我的代码,现在它只为当前用户(常驻登录)生成。谢谢!
发布于 2016-06-08 10:53:14
到哪里去找那家旅店的所有居民:
residents = Resident.where(hostel: Hostel.where(name: current_admin_user.hostel).first.id)循环遍历居民并为每个居民创建账单:
residents.each do |resident|
# Note here I use bill_params (because it's in your example), but I'm not totally sure it's the behavior you want
resident.bills.create(bill_params)
end请注意,您使用的是create,所以您正在使用的@bill.save是不必要的(无论如何,create调用save,参见Differences between new + save and create)。要检查账单是否已经成功创建,可以使用@bill.persisted? (参见:Determine if ActiveRecord Object is New)。
https://stackoverflow.com/questions/37700232
复制相似问题