需要为[=10th=]的所有用户生成账单,创建方法应该是什么?
Need to genrate bills for all users in rails, what should be create method?
嗨,
我需要为我的应用程序中的所有用户(居民)生成账单,bill_controller 的 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
The active admin users can generate bills, and bills will be generated
only for residents that have same hostel with admin user ! And bills
should be generated for all residents with specific hostel. check out
my code, right now it is generating only for current user(resident
logged in) . thanks !
使用 where 获取该旅馆的所有居民:
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). To check if the bill has been successfully created you could use @bill.persisted?
for instance (see : Determine if ActiveRecord Object is New)。
嗨,
我需要为我的应用程序中的所有用户(居民)生成账单,bill_controller 的 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
The active admin users can generate bills, and bills will be generated only for residents that have same hostel with admin user ! And bills should be generated for all residents with specific hostel. check out my code, right now it is generating only for current user(resident logged in) . thanks !
使用 where 获取该旅馆的所有居民:
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). To check if the bill has been successfully created you could use @bill.persisted?
for instance (see : Determine if ActiveRecord Object is New)。