ruby 控制台 - 活动记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 控制台 - 活动记录相关的知识,希望对你有一定的参考价值。




# add references

rails g migration add_references_to_items  

rake db:migrate


---


# add attributes

rails g migration add_name_to_users name:string

rake db:migrate


rails g migration add_attributes_to_match winner_faction:string loser_faction:string duration:integer start:date


----


# Create join table 

rails g migration CreateJoinTableCategorySnippet category snippet

rake db:migrate


string - is for small data types such as a title. (Should you choose string or text?)
:text - is for longer pieces of textual data, such as a paragraph of information
:binary - is for storing data such as images, audio, or movies.
:boolean - is for storing true or false values.
:date - store only the date
:datetime - store the date and time into a column.
:time - is for time only
:timestamp - for storing date and time into a column.(Whats the difference between datetime and timestamp?)
:decimal - is for decimals (example of how to use decimals).
:float - is for decimals. (Whats the difference between decimal and float?)
:integer - is for whole numbers.
:primary_key - unique key that can uniquely identify each row in a table
rails g model User name:string email:string task_id:integer
curl -XGET localhost:3000/users
[{"id":1,"name":"bob","email":null,"created_at":"2015-04-19T22:00:03.075Z","updated_at":"2015-04-19T22:00:03.075Z"}]

curl -XPOST localhost:3000/users/1/tasks -d '{"name":"rails generate"}' --header 'Content-Type: application/json'
# should create a new task 

# replace `:id` with an id of a task that you have
curl -XPUT localhost:3000/users/1/tasks/:id --header 'Content-Type: application/json'
# should return a task with `completed` set

# replace `:id` with an id of a task that you have
curl -XDELETE localhost:3000/users/1/tasks/:id

# replace `:id` with an id of a task that you just deleted
curl -XGET localhost:3000/users/1/tasks/:id -v
# should return an error
#open
Rails c

#close
exit


Creating new user on the console

u = User.new
u.save

User.all

---

Creating new exercise on the console

e = Exercise.new
e.save

Exercise.all

---

Update

u = User.find(1)
u.exercise_id = 1

u.save

---

Find the first exercise of the user Steve

User.where(name: 'Steven').first.exercises

----

Find the all exercise of the user Steve

User.where(name: 'Steven').exercises

以上是关于ruby 控制台 - 活动记录的主要内容,如果未能解决你的问题,请参考以下文章

ruby 使用ssl加密的活动记录

ruby 活动记录和迁移单个文件

ruby rails3列出活动记录表

如何使普通的 ruby​​ 对象可分配为活动记录关联

Ruby活动记录在mysql中插入日期时间而不是时间

Ruby on Rails 活动记录查询(.each、.collect、.map ...?)