[Rail Level1] CRUD
CRUD
2023-09-14 09:00:56 时间
Accessing tables:
If you have a table: 'tweets' (Lowercase & Plural Table Name).
Outside you can access the table by
t = Tweet.find(3)
It should be Singular & Uppercase Table Name.
Create:
t = Tweet.new t.status = "I <3 brains." t.save or t = Tweet.new( status: "I < 3 brains", zombie: "Jim" )
t.save or Tweet.create(status: "I <3 brains", zombie: "Jim")
Read:
Tweet.find(2) Tweet.find(3,4,5) Tweet.first Tweet.last Tweet.all Tweet.count Tweet.order(:zombie) Tweet.limit(10) Tweet.where(zombie: "ash")
Update:
t = Tweet.find(3) t.zombie = "EyeballChoper" t.save or t = Tweet.find(2) t.attributes = { status: "Can I munch you eyeballs?", zombie: "EyeballChopmer" } t.save or t = Tweet.find(2) t.update( status: "Can I munch your eyeballs?", zombie: "EyeballChomper")
Delete:
t = Tweet.find(2) t.destory or Tweet.find(2).destroy or Tweet.destroy_all
Method Chaining:
Tweet.where(zombie:"ash").order(:status).limit(10) Tweet.where(zombie: "ash").first