Skip to main content
8 votes
Accepted

Finding users who liked a post

Use joined tables to make more performant database calculations and avoid unnecessary models instantiations. ...
mikdiet's user avatar
  • 301
3 votes

ActiveRecord selection, modified by optional parameters

Some notes: That's too much code for a controller method, it should be moved to the model. The same name (@measurements) holds up to 4 different values. As a ...
tokland's user avatar
  • 11.1k
3 votes
Accepted

A method that shows maps users are a part of

This would potentially be a very expensive way to do this as you have to query the dtabase once for every group the user is in, i.e. an n+1 query. You should be able to do it with a single query by ...
Marc Rohloff's user avatar
  • 3,477
3 votes
Accepted

Update join table using list of checkboxes in Rails

Actually, Active Record can work with joined collection (including has_many :through) from the box. So, you just need to pass new collection instead of old, and AR will delete excessive and add new ...
mikdiet's user avatar
  • 301
3 votes
Accepted

Dynamically combine ActiveRecord Relation results

Turns out the last code snippet in my question was close to what I ended up needing: ...
kaydanzie's user avatar
3 votes
Accepted

Rails: create a relationship between comment and user

Don't think your question belongs to this code review section. You would have gotten help much faster if had posted in stackoverflow instead. Anyway there are couple points you need to check. The ...
Thang's user avatar
  • 166
2 votes

SE-like voting system

First of all, I'd strongly encourage you to change your vote-scheme to use a value attribute of either 1 or ...
Flambino's user avatar
  • 33k
2 votes

Simple ActiveRecord attributes encryption in Rails 5+

There is already a gem for encrypting data at rest. Does this work for you? https://github.com/attr-encrypted/attr_encrypted
Ram on Rails's user avatar
2 votes
Accepted

Simple ActiveRecord attributes encryption in Rails 5+

Some notes: The recommended way to overwrite accessors is to use super. As @Flambino says, it's better not to hardcode secrets directly in the code. It can be a ...
tokland's user avatar
  • 11.1k
2 votes
Accepted

Rails query for all questions in tournaments that a user participates in

Ok, so based on what your said your associations are you can do ...
j-dexx's user avatar
  • 136
2 votes

Fetching items from database with "Outstanding" status

As far as I can see this is a very old Rails code. Please follow these steps: Refactor the code a little bit: ...
coorasse's user avatar
  • 184
2 votes
Accepted

Build a pizza and choose your toppings

Saving customized pizzas this way, looks OK but it also depends on how the rest of application is structured. I think your biggest concern here should be the historical data integrity you mentioned in ...
Ioannis Tziligkakis's user avatar
2 votes
Accepted

Seed a database

On a technical note create! will always return a valid object (or raise an exception) What I usually do is put it in a separate method and use an early return: <...
Marc Rohloff's user avatar
  • 3,477
2 votes

Display related records, if they exist, in Rails view

You could also use first_or_initialize. It tries to find the record and, if not found, creates a new unsaved object (so that you always have an object and don't ...
Sergio Tulentsev's user avatar
2 votes

Count has_many relationships

You can make use ":counter_cache", it finds the number of belonging objects more efficient. ...
Neha Chopra's user avatar
2 votes

Rails method to match users according to language fluency level

I think you should be closer to MVC here. All this code should be in model, so from controller you just need to command user email matches like @user.email_matches. ...
effect305's user avatar
2 votes

Rails method to match users according to language fluency level

This is a somewhat shorter more concise version of your code. Taking advantages of some or Rails's magic and conventions. ...
Eyeslandic's user avatar
2 votes

Display related records, if they exist, in Rails view

I can see three problems with this example. 1. You are doing DB queries in the View template A simple rule of thumb with Rails views: avoid triggering DB queries in your views at all costs. The ...
Adam Lassek's user avatar
2 votes
Accepted

Extracting queries from controller to model

This is a good place to use scopes. http://edgeguides.rubyonrails.org/active_record_querying.html#scopes Scopes are just syntactic sugar for class methods that return relations. In this way they can ...
kjprice's user avatar
  • 196
2 votes

ActiveRecord query for coaches a client has bookmarked

if you don't need the bookmarks instances then I would be inclined to go for something like: ...
Marc Rohloff's user avatar
  • 3,477
1 vote

Counting number of messages for a mailbox over several periods

As David points out you are going to run four queries. The size method will actually run more efficient sql, and not load the objects into memory which is also more ...
Marc Rohloff's user avatar
  • 3,477
1 vote
Accepted

Counting number of messages for a mailbox over several periods

Some suggestions ... Use a counter cache for messages on Mailbox -- the count of all can be read from the ...
David Aldridge's user avatar
1 vote

Display related records, if they exist, in Rails view

Using try method of ruby, exception will not be raised and nil will be returned instead, if the receiving object is a nil object....
puneet18's user avatar
  • 111
1 vote

Display related records, if they exist, in Rails view

Is this much logic in view is justified to check for the presence of record? It is justified to check for a presence of a record in the view but you are writing AR queries in the view which will ...
Pavan's user avatar
  • 705
1 vote

Seed a database

All of the ifs checking to make sure the object is created are superfluous and hurt readability. If the call to create! fails, ...
Greg Burghardt's user avatar
1 vote

Count has_many relationships

I would suggest you go with counter cache for this scenario. If you don't regularly update the values of a product, then use counter cache. You can combine the three ...
Selvamani's user avatar
  • 111
1 vote

Update single common column in multiple has-many relationships rails

since you have to cache price_type on every table, you could use callbacks to not have to do this in a... controller action? or wherever. This could be a first step ...
Chris Drappier's user avatar
1 vote

Update single common column in multiple has-many relationships rails

(Converted a comment into this answer) If all the records should use @private_school.price_type as their own price_type, then ...
Flambino's user avatar
  • 33k
1 vote

Build a pizza and choose your toppings

I'd say the best way to express this would be to add a boolean flag column to toppings which indicates whether the topping is currently available to be ordered. ...
Ray Hamel's user avatar
  • 268

Only top scored, non community-wiki answers of a minimum length are eligible