Chee Chong Hwa aka CCH, a Malaysian Chief Software Architect blogs on his experiences with Ruby on Rail running on Win2003 Server+Apache 2.26 environment since 2007. Flirted briefly with Ubuntu 9.04 in 2009. Since January 2017, now using RubyMine IDE/Notepad++ to work with Ruby 2.26 +Rails 5.01+Apache 2.4.23 + MySQL 5.7.14 on Windows 10 64-bits.
Wednesday, February 8, 2017
Tuesday, February 7, 2017
Useful Rails 5 Expressions
Select All Fields (columns) in a Tabledef index @orders = Order.all endSelect Some Fields (columns) in a Tabledef index @orders = Order.select(:product, :price, :quantity) endMore at http://blog.bigbinary.com/2016/05/24/rails-5-adds-active-record-ignored-columns.html Display All Rows in a Tabledef list @orders = Order.all endThe @orders = Order.all line in the list method tells Rails to search the orders table and store each row it finds in the @orders instance object. Display Target Row in a Tabledef show @orders = Order.find(params[:id]) endThe @orders = Order.find(param[:id]) line in the show method tells Rails to search the orders table and store the target row it finds in the @orders instance object.
What happens when you generate a scaffold in Rails 5 ?
Q. What happens when you rails g scaffold Employee eno:string ename:string dept:string date_join:date active:boolean ?
ANSWER
1. Migration file created as yyyymmddhhmmss_create_employees.rb
CreateEmployees < ActiveRecord::Migration[ 5.0]
def change
create_table :employees do |t|
t.string :eno
t.string :ename
t.string :dept
t.date :date_join
t.boolean :active
t.timestamps
end
end
end
2. Employee Model created as employee.rb
class Employee < ApplicationRecord end
3. Employees Controller created as employees_controller.rb
class EmployeesController < ApplicationController before_action :set_employee, only: [:show, :edit, :update, :destroy] # GET /employees # GET /employees.json def index @employees = Employee.all end # GET /employees/1 # GET /employees/1.json def show end # GET /employees/new def new @employee = Employee.new end # GET /employees/1/edit def edit end # POST /employees # POST /employees.json def create @employee = Employee.new(employee_params) respond_to do |format| if @employee.save format.html { redirect_to @employee, notice: 'Employee was successfully created.' } format.json { render :show, status: :created, location: @employee } else format.html { render :new } format.json { render json: @employee.errors, status: :unprocessable_entity } end end end # PATCH/PUT /employees/1 # PATCH/PUT /employees/1.json def update respond_to do |format| if @employee.update(employee_params) format.html { redirect_to @employee, notice: 'Employee was successfully updated.' } format.json { render :show, status: :ok, location: @employee } else format.html { render :edit } format.json { render json: @employee.errors, status: :unprocessable_entity } end end end # DELETE /employees/1 # DELETE /employees/1.json def destroy @employee.destroy respond_to do |format| format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_employee @employee = Employee.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def employee_params params.require(:employee).permit(:eno, :ename, :dept, :date_join, :active) end end4. Views created under Views/employees folder
_form.html.erb, edit.html.erb, new.html.erb and show.html.erb automatically created5. Tests folder
Test files for controllers employees_controller_tests.rb Test files for models employees_tests.rb Test files for fixtures employees.yml6. Routes: resources:employees
Monday, February 6, 2017
Given a Model, how to loop through all properties?
Q. I want to loop through all the properties of my 'user' model, how can I do this ?
A. If you have an instance of your model then
A. If you have an instance of your model then
user.attributes is a Hash of the model's attributes and their values so, for example, you can do something like:user.attributes.each_pair do |name, value|
puts "#{name} = #{value}"
end
If you don't have a specific instance then the class has methods that return information about the fields in the database e.g.
User.columns and User.content_columns. e.g.User.columns.each do |column|
puts column.name
end
How to loop through every record in a Database Table ?
Here is the correct syntax to iterate over all User Table :
Also, to improve performance, you may want to use
(see doc) instead of
Also, to improve performance, you may want to use
User.find_each(see doc) instead of
User.allUser.all.each do |user|
#the code here is called once for each user
# user is accessible by 'user' variable
end
User.find_each do |user|
#the code here is called once for each user
# user is accessible by 'user' variable
end
Subscribe to:
Posts (Atom)
Welcome to Rails.. Rails... Rails !
In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.
As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam
CCH
10th June 2010, 19:42
As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam
CCH
10th June 2010, 19:42








