important tasks in rails
1. add csv file in rails import --
2. update custom id in rails project
3. bulk_destroy --
def bulk_destroy
users = Account.where(id: params[:ids].split(","))
return no_records_found unless users.any?
if users.destroy_all
render json: { message: 'Users was successfully destroyed.' }, status: :ok
else
render json: { errors: users.errors }, status: :unprocessable_entity
end
end
4. bulk_update --
def bulk_update
users = Account.where(id: params[:ids])
if users.any?
users.each do |user|
user.update(account_params)
end
render json: AccountSerializer.new(users, meta: {message: "Users successfully Updated."}).serializable_hash, status: :created
else
no_records_found
end
end
5. export csv file in rails ::
def export
csv_data = Account.order(:id)
respond_to do |format|
format.csv { send_data csv_data.to_csv, filename: "User-#{DateTime.now}.csv" }
end
end
6. search in rails ::
def search
accounts = Account.where('first_name ILIKE ?', "%#{params[:name]}%").order(id: :desc).paginate(page: params[:page], per_page: 20)
if accounts.present?
render json: AccountSerializer.new(accounts, meta: {total_pages: accounts.total_pages, message: "User search list"}).serializable_hash, status: :ok
else
no_records_found
end
end
7.
Comments
Post a Comment