`
peryt
  • 浏览: 55641 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

6.1 play with the newly created model.

阅读更多

in prev chapter, we created a user model, now we will play with it.

 

1. first, let's start rails console,

 

this time we will use a param: --sandbox


this param make sure when you exit the console, all changes made here will be rolled back!!!

 

rails console --sandbox

 

(if you want to play with test database, you can

 

rails console test

 

2. I have a very good habit that when play with the console, I will keep a log terminal open, to see what is happening on the log.

 

use this command to power up the log terminal:

 

tail -f log/development.log


-f param will make sure it will add newly added lines to the terminal when new lines created.

 

3. after you start rails console, Rails will auto load rails environment, so models and controllers are all loaded.

you can use them directly

 

4. for a model that inherit from active record, you can initialize it using a hash that include the value of the attributs.

 

for example:

user = User.new(:name => "abcd", :email => "abcd@abcd.com")

 

to save this record, you need to call:

 

user.save

 

this line will save the user info into database.

 

this method return true if save successful, return false if the save fails.

 

5. user = User.new

user.save

 

these two lines can create and save a user record.

 

there is anoter way that will create and save by one line of code:

 

user = User.create(:name => "abcd", :email => "abcd@abcd.com")

note: User.create will return the object itself.

 

6. the opposite side of User.create is user_object.destroy

 

user.destroy

 

this method will also return the destroied user object.

(but in general, I have never find this returned destroied object of any use. this object is still in memory after destroy)

 

 

7. next we will learn "find" method that will be used everyday:

 

a. User.find(1) =======> find by user id

 

b.  a more genral use of find is finding by a colum:

 

User.find_by_email "abcd@abcd.com"

(but, will the effieciency very slow to search through database by find by email? you are right, we will solve it by adding index to database later!!!)

 

c. User.first, this is another find.

 

d. User.all, this will return an array.

 

 

8.  next we will learn how to update user object.

 

a. user = User.find_by_email "abcd@abcd.com"

user.name = "abcd2"

user.save

 

note, "user.save" is need to save it into database!!!

 

without it, it will not get saved, we can see this fact by using user.reload:

 

user = User.find_by_email "abcd@abcd.com"

user.name = "abcd3"

user.reload.name   ====> "abcd", 

 

 

b. another way of updating user object is using a method called, update_attributes

 

user.update_attributes(:name => "def", :email  => "ghz@ghz.com" )

 

will return true if update success

 

this method will accept a hash as param, using the hash to update records.

 

one thing to note:  if you used to define any attr_accessible attributes, then only columns that belong to attr_accessible can be updated using this method

 

 

 

 

 

 

 

 

0
2
分享到:
评论

相关推荐

    Packt.Getting.Started.with.Tableau.2019.2.2nd.Edition.rar

    Get up and running with the newly released features of Tableau 2019.2 Create enterprise-grade dashboard and reports to communicate your insights effectively Begin your Tableau journey by ...

    Learning.Embedded.Linux.using.the.Yocto.Project.1784397393

    It will present the meta-realtime layer and the newly created meta-cgl layer, its purpose, and how it can add value to poky. Table of Contents Chapter 1. Introduction Chapter 2. Cross-compiling ...

    The EM Algorithm and Extensions (2nd Edition)

    The text includes newly added and updated results on convergence, and new discussion of categorical data, numerical differentiation, and variants of the EM algorithm. It also explores the ...

    photovoltaic-simulator-7-paramete.zip_PV SYSTEM MPPT_mppt pv arr

    The output characteristics ... The proposed PV simulator will facilitate the design aspects of PV systems and help in behaviorassessment of newly developed controllers prior to practical implementation.

    PLSqlsample

    When the filter_records.sql is run, it fetches all the records into a collection and further processed to separate the newly created orders and orders that are already fulfilled. The criteria used ...

    1024点FFT快速傅立叶变换

    2. unzip the FFT design files into the newly created project directory 3. Open the VHDL file 'fftwrap.vhd' in the Foundation HLD editor 4. In the HDL editor create a schematic macro symbol by ...

    python3.6.5参考手册 chm

    PEP 446: Newly Created File Descriptors Are Non-Inheritable Improvements to Codec Handling PEP 451: A ModuleSpec Type for the Import System Other Language Changes New Modules asyncio ensurepip ...

    Microsoft.Excel.VBA.Professional.Projects.(2003).LiB

    The book is divided into six parts, with Part I providing an introduction to the VBA language and the Excel object model. Parts II through V each consist of three to four chapters that introduce new ...

    A PACKING GENERATION SCHEME FOR THE GRANULAR.pdf

    Each particle's packing location, contact-point co-ordinates, and three-dimensional graphs can be created using the packing domain given boundaries, along with numbers, and geometrical information of...

    独立主格结构和with复合结构.pptx

    3. "With a lot of difficult problems to settle, the newly-elected president is having a hard time." 这里的"problems to settle"表示将来要解决的问题,总统因此面临困难。 4. "She sleeps with the window ...

    addison.wesley.opengl.shading.language.2nd.edition.jan.2006

    Chapter 4 discusses how the newly defined programmable parts of the rendering pipeline interact with each other and with OpenGL's fixed functionality. This discussion includes descriptions of the ...

    Windows Graphic

    Provides information about programming with the High Level Shading Language for DirectX. Using HLSL, you can create C like programmable shaders for the Direct3D pipeline. Tools for DirectX Graphics...

    Learn JavaScript with p5.js_Coding for Visual Learners-Apress(2018).pdf

    whatever field that you would like with your newly established skill set. The skills that you will acquire from this book are highly transferrable and can be used with whatever you choose to build: ...

    四川省雅安中学2017-2018学年高二英语下学期期中试题

    18. What does the speaker say about the book "Gone with the Wind"? A. It was written by a famous French author. B. It has been translated into many languages. C. It is based on a true story. 19. When...

    AIS-PSO.rar_PSO-AIS_The Space Between_immunity algorithm_optimiz

    Particle Swarm Optimization (PSO) is a newly-emerging heuristic global search algorithm based on swarm intelligence and it searches the global optimal point in the complex search space through the ...

    数位板压力测试

    • The tablet must work with legacy applications, and with applications written to take advan¬tage of tablet services. • The tablet driver must add minimal speed and memory overhead, so as many ...

    CoreOS Essentials(PACKT,2015)

    You will also see how to set up and use CoreUpdate and Enterprise Registry, and get an introduction to the new App Container called rkt and the newly introduced cluster manager known as Kubernetes. ...

    With的用法详解[参照].pdf

    再来看一个例子:"With a lot of difficult problems to settle, the newly-elected president is having a hard time." 这里的不定式"to settle"表示的是总统即将要解决的问题,主动式表示了行动的意图和目的。...

Global site tag (gtag.js) - Google Analytics