ails g controller Say hello goodbye //camel case controller
@time = Time.now
<%=
link_to “anchor text”, [nama-controller]_[nama-view]_path
%>
orm -> object relationship mapping
tabel = orders , class = order
rows di tabel = object dr class
ages = []
for person in @people
ages << person.age
end
======
rails g scaffold Product title:string description:text image_url:string price:decimal
t.decimal :price, precision: 8 , scale: 2
=======
###cara gunain seed
db/seeds.rb
Product.delete_all
Product.create!(title: 'programing ruby', description:%{<p>ruby programing<p>}, image_url: ruby.jpg, price: 2.00)
“(Note that this code uses %{…}. This is an alternative syntax for double-quoted string literals, convenient for use with long strings. Note also that because it uses Rails’ create! method, it will raise an exception if records cannot be inserted because of validation errors.)”
Excerpt From: Sam Ruby, Dave Thomas, David Heinemeier Hansson. “Agile Web Development with Rails 4 (for Lorinda Hartzler).” iBooks.
rake db:seed
=====
### cara gunain git
git init
git add .
git commit -m “Depot Scaffold”
git checkout . -> ngembaliin ke state terakhir
======
###validation
app/models/product.rb
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
=====
#cara gunain fixtures
fixtures :products
one:
title: MyString
description: MyText
image_url: MyString
price: 9.99
two:
title: MyString
description: MyText
image_url: MyString
price: 9.99
======
## adding path and store
as:’store’ —>> store_path
======
## cara ngaktifin caching di development!
rails40/depot_e/config/environments/development.rb
config.action_controller.perform_caching = true”
======
model and references
http://guides.rubyonrails.org/association_basics.html
migration
http://guides.rubyonrails.org/active_record_migrations.html
======
#bikin references
rails g scaffold LineItem product:references cart:belongs_to
-pada level model tdk ada perbedaan antara references sama belongs_to
lineitem belongs_to :product
lineitem belongs_to :cart
“The database now has a place to store the references between line items, carts, and products.”
“here’s an easy way to remember where to put belongs_to declarations: if a table has foreign keys, the corresponding model should have a belongs_to for each.”
>> di models/cart.rb
has_many :line_items, dependent: :destroy
#setiap record di cart dependent terhadap linitem shg apabila dihapus ikt terhapus.
#cart mempunyai berpotensial untuk memiliki banyak lineitems
>> di models/product.rb
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
#ngasih hubungan antara product & line_items
#memastikan ketika ada item di delete di line items, product ga ikt kedelete.
“Here we declare that a product has many line items and define a hook method named ensure_not_referenced_by_any_line_item . A hook method is a method that Rails calls automatically at a given point in an object’s life. In this case, the method will be called before Rails attempts to destroy a row in the database. If the hook method returns false, the row will not be destroyed.
Note that we have direct access to the errors object. This is the same place that the validates stores error messages. Errors can be associated with individual attributes, but in this case we associate the error with the base object.”
=====
rails generate migration add_quantity_to_line_items quantity:integer
No comments:
Post a Comment