开发者

Rails SQL create table syntax

I just learned to use ActiveRecord and change things in the database. In the video I watched it instructed me to use this command in the terminal (after writing rails console):

subjects = Subject.new(:name => "First Chapter", :position => 1, visible => true)

I was wondering what exactly the role of the wo开发者_StackOverflow社区rd subjects in the beginning is, and what the role of the second Subject (capitalized) is. I believe the capitalized one is a class, but how exactly does everything work? could I just write a= Subjects.new... or does it have to say subjects?

Also, do singular and plural matter? How about capital and non capital? I got really confused when to use capital and when to use plural.

Finally, I used subject.destroy to delete an entry. Does that destroy the last object that I found using subject.find(2) for example? And is it possible to recover the data after deleting it?

Final final question is, after using subject.destroy I did a new entry but it appeared to have skipped one ID. I erased ID=2 entry, and the next I entered got the ID 3 (which is logical). If I write subject = Subject.find(2) it tells me there is nothing found. So is there a way to shift the entry with ID 3 to the position of ID 2? That would close the hole, since on ID 2 there is nothing anyway.

Sorry for the many questions, but with little experience in ruby and SQL it's quite tough.

FYI my table looks like this now:

Subject.all

=> [< Subject id: 1, name: "Initial Subject", position: 1, visible: true>, 

< Subject id: 3, name: "Second Subject", position: 2, visible: false>, 

< Subject id: 4, name: "third Subject", position: 3, visible: false>,

< Subject id: 5, name: "Revised Subject", position: 4, visible: true>]


I think you will be able to answer most of your questions by reading through official guide.

http://guides.rubyonrails.org/getting_started.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜