Skip to content

Instantly share code, notes, and snippets.

@makshark
Created October 19, 2015 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makshark/34851800d719acb4bce1 to your computer and use it in GitHub Desktop.
Save makshark/34851800d719acb4bce1 to your computer and use it in GitHub Desktop.
1. In the previous code, everything is fine. You can order you records by adding order method and send it field (id).
class User < ActiveRecord::Base
scope :find_by_age, ->(age) { where("users.age = #{age}").order(:id) }
end
2.I assume that the developer incorrectly calculates the number of posts. If a user has a large number of posts, we need to add a callbacks to posts model. In this callbacks we will be increment or decrement users field posts_count. And when we will be call search we can increase our perfomance.
3. Write test for
class Message
def mark_as_read
update_column :read, true
end
end
Test.
describe Message do
it 'should update column read'
Message.create(name: 'my_example', read: false)
message = message.last.update(read: true)
expect(message.read).to eq (true)
end
end
4. When we use includes it is allow us to avoid n+1 problem and this way contain our model association. When we use :joins - this way joins tables together in sql.
5. ssh-keygen -t rsa -b 4096 -C "write_your_email_here@my.com". After generating we must add this key to remote machine to file (for exaple authorise keys). After thet remote machine will know us) And we will login automatic.
6. When we use ! that allow us immediatly change object attribute.
7. Many ways to solve this issue). For example, we can add code, which will be contain user and his account, and we will be check, if user session id equal id which user want to change, then allow that operation. Ofc we need to add authentication for users, so we can identify the user. Each user can change only his information (email and etc.) Also, we can add flash-notice, when user try enter incorrect id. Also, we can delete user id from form) and get it from session (fox example).
8. There are crucial differences between relational and no-sql databases.
relation - 1, no-sql - 2 (lets call them so)
1 - Ralation (allow to use relation algebra)
1 - based on tables, 2 - based on document which have key-value pairs, graph databases.
1 - have static schema, 2 - dynamic schema
1 - are vertically scalable, 2 - are horizontally scalable
1 - uses SQL for creating queries, 2 - uses UnSQL
if we need organize hierarchical data i prefer to uese NoSQL (for example binary tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment