`
peryt
  • 浏览: 52925 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • 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 ...

    Game.Development.and.Simulation.with.Unreal.Technology.149870624X

    This is one of the first books on developing interactive content for games, entertainment, and simulation to cover the newly released UE4. Industry-level game concepts in level design, coding, and ...

    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 ...

    Design of High-speed Burst Mode Clock and Data Recovery IC

    Design of a high bit rate burst mode clock and data recovery (BMCDR) circuit for gigabit passive optical ...developed behaviour model is verified by comparing with the detailed circuit simulation.

    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 ...

    a five-factor asset pricing model.pdf

    They acknowledge the major contributions Fama and French have made to the literature in the past and so studied this new research with great interest. However the debate is set to continue – they ...

    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 ...

    CACTI 6.0: A Tool to Understand Large Caches.pdf

    Future processors will likely have large on-chip caches with a possibility of dedicating an entire die for on-chip storage in a...assumed for the newly added modules along with their validation analysis.

    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...

    Packt.Practical.DevOps

    Chapter 1, Introduction to DevOps and Continuous Delivery, deals with the background of DevOps and sets the scene for how DevOps fts in the wider world of Agile systems development. Chapter 2, A View ...

    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: ...

    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 ...

    Handbook-of-Hidden-Markov-Models-in-Bioinformatics.pdf

    This book is designed for intermediate to advanced bioinformatics students who have taken a couple of introductory courses, along with the prerequisite biochemistry and computer science introductions....

    数位板压力测试

    • 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 ...

Global site tag (gtag.js) - Google Analytics