Ruby on Rails, especially Rails is always difficult to understand for beginners. I made enjoyable quiz flashcards for beginners. You can repeat quiz by yourself like leaning foreign languages.
3. Let’s learn Rails in easy way
Rails is so difficult for beginners like me.
I always feel there should be very easy documentation explaining Rails.
I will try to explain the basics of Rails in a easy way.
My idea is to create simple flash cards like when we memorize foreign languages.
Please use the following Q&A with your friend, asking each other.
3
4. What is hash in rails?
As well as array, hash is an object to manage multiple objects.
Example : hash1 = { “Andy” => 28, “Bob” => 30, “Keith” => 32, ……..}
Key: Andy, Bob, Keith…
Value: 28, 30, 32….
variable = {key, value} hash1 = { “Andy”, 28 }
variable[:key] = value hash1[“Andy”] = 28
4
5. Let’s take an example of database table called “tweets”
This is like an excel sheet.
id text user
1 I’m fine today. Andy
2 I’ve got married! Bob
3 I’ve got new job. Keith
5
*Table name is always lowercase & plural like “tweets”.
46. Q19: Find all tweets from user named “Bob”
A: Tweet.where(user:”Bob”)
*Tweet needs to be singular and uppercase
46
47. Q20: Find only tweets from user named “Bob”
ordered by text, only the first 10.
47
48. Q20: Find only tweets from user named “Bob”
ordered by text, only the first 10.
A:Tweet.where(user:”Bob”).
order(:text).limit(10)
*This is called method chaining
*Tweet needs to be singular and uppercase
48
49. Q21: Find only tweets from user named “Bob” just
the first one.
49
50. Q21: Find only tweets from user named “Bob” just
the first one.
A:Tweet.where(user:”Bob”).
first
*This is called method chaining
*Tweet needs to be singular and uppercase
50
52. Q22: Find 3rd tweet and pass it to t. Update its user
as “Andy”.
52
53. Q22: Find 3rd tweet and pass it to t. Update its user
as “Andy”.
A: t=Tweet.find(3)
t.user = “Andy”
t.save
*Tweet needs to be singular and uppercase 53
55. Q23: Give me a recipe of previous codes.
A: t=TableName.find(id)
t.key = value
t.save
*TableName needs to be singular and uppercase 55
56. Q24: Find 2nd tweet and pass it to t. Update its text
as “Hello” and user as “Andy”.
56
57. Q24: Find 2nd tweet and pass it to t. Update its text
as “Hello” and user as “Andy”.
A: t=Tweet.find(2)
t.attributes = {
text: “Hello”
user: “Andy” }
t.save
*Tweet needs to be singular and uppercase 57
59. Q25: Give me a recipe of previous codes.
A: t=TableName.find(id)
t.attributes = hash
t.save
*TableName needs to be singular and uppercase 59
60. Q26: Find 2nd tweet and pass it to t. Update its text
as “Hello” and user as “Andy”. Use update method.
60
61. Q26: Find 2nd tweet and pass it to t. Update its text
as “Hello” and user as “Andy”. Use update method.
A: t=Tweet.find(2)
t.update = {
text: “Hello”
user: “Andy” }
t.save
*Tweet needs to be singular and uppercase 61
63. Q27: Give me a recipe of previous codes.
A: t=TableName.find(id)
t = TableName.update(hash)
t.save
*TableName needs to be singular and uppercase 63