Skip to content

Instantly share code, notes, and snippets.

@pecha7x
Last active October 25, 2015 18:50
Show Gist options
  • Save pecha7x/9dd89d299ac9bc4b9595 to your computer and use it in GitHub Desktop.
Save pecha7x/9dd89d299ac9bc4b9595 to your computer and use it in GitHub Desktop.
# (1) Is there something wrong with the following code:
class User < ActiveRecord::Base
scope :find_by_age, ->(age) { where("users.age = #{age}") }
end
# If yes, please tell why. If not, please tell how to order all records by id.
-----
Resp: yes, it's wrong. somebody forgot set '' for arg and AR will generate wrong select.
solutions:
scope :find_by_age, ->(age) { where("users.age = '#{age}"') }
or
scope :find_by_age, ->(age) { where("users.age = ?"), age }
or better(not need set "users" table, because we already in User)
scope :find_by_age, ->(age) { where(age: age) }
+ AR already have methods find_by_* ('*'- table column) so this scope is excess.
-----
# (2) One developer has added posts_count column to the users table. After this operation he found that
# he has to wait longer for the search results. What could be the reason of the performance issues? What did he forget?
-----
Resp:
He forgot add couter cache for User-association in Post model -
belongs_to :user, counter_cache: true
-----
# (3) Please write a test in the rspec for the following code:
class Message
def mark_as_read
update_column :read, true
end
end
-----
Resp:
describe Message do
it 'should change read column for message' do
lambda { subject.mark_as_read }.should change { subject.read }.by(true)
end
end
-----
# (4) What's the difference between includes() and joins() in the ActiveRecord?
-----
Resp:
joins() will generated select with INNER JOIN and by this way loaded data only from "main" table.
includes() will generated select with LEFT OUTER JOIN and by this way loaded data also for associations tables.
-----
# (5) Please present, how you can generate new ssh key and how to set up automatic login on the remote machine.
-----
Resp:
on local mashine I'll generate auth keys and copy it to remote machine:
$ ssh-keygen -t rsa
$ cat .ssh/id_rsa.pub | ssh remote_user@remote_machine 'cat >> .ssh/authorized_keys'
-----
# (6) What's the difference between update_attributes! and update_attributes ?
-----
Resp:
update_attributes! will raise exception if record will be not valid (like save!)
-----
# (7)
<form action='/user' method='post'>
<input type='hidden' name='user[id]' value='4932'>
<input type='text' name='user[name]' value='Jan Kowalski'>
<input type='text' name='user[email]' value='jan@kowalski.pl'>
<input type='submit' value='Update My Account'>
</form>
# The developer found that when he changes the user[id] field, he can read the name and e-mail for the different user.
# What could be the reason of the following situation? How can you fix that?
-----
Resp:
Do you mean update data not only read right?
solution - not need use logic like this. CRUD actions for "same" datas need organize via Auth module(user_sessions or user_access_token)
-----
# (8) What's the difference between relational and no-sql databases?
-----
Resp:
I want mark only main next differences -
1) relational DB is table based DB. no-sql DB is doc based, key-value pair. this fact very affected on DB-schema.
2) relational DB is vertically scalable - increasing server resources on single server. no-sql DB is horizontally scalable - just add new servers to db structure.
* 3) I think that relational DB is better for heavy transactional type applications. because is provide atomicity and integrity of data.
-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment