Type a keyword and hit enter to start searching. Press Esc to cancel.

Posts Tagged ‘rails’

The perfect database schema is one where you don’t need to concede form to any obscure requirements. In that perfect scenario all associated models could be individually identified with a single foreign key.   According to the Rails guides, you can only define an association’s foreign key with a single column. We live in an imperfect world with many obscure business requirements, so sometimes we need to use two keys.  One common scenario is when you need to centralize and synchronize data from multiple outside API’s.  Luckily, there is a solution. This example will be based on an Entity Attribute […]

Using an AngularJS based frontend, paired with a Rails backend, is a great stack.  AngularJS provides $resource, which is a mostly complete module to interface with the Rails backend and provide CRUD actions.  However, if you are using strong parameters with a require statement in Rails, then $resource can be a bit of a pain. When generating forms with ActionView, you use form_for @model, this will generate input names with a name like name=”model[attribute]”.  Using this same naming scheme in AngularJS does not work, because the input names are not correctly interpreted as a JSON object. So, instead of using $resource, […]

ActiveRecord is great.  It helps hide away a lot of trivial SQL when creating applications with Rails.  It’s fairly good at what it does to and it’s constantly growing and improving.  However, it falls short when it comes to creating a query that combines multiple calculations.  As an example, let’s get an array of the total sales for every month for the last 12 months of Orders. totals = [] 12.times do |i| month = i.months.ago totals << Order.where(‘created_at >= ? AND created_at <= ?’, month.beginning_of_month, month.end_of_month).sum(:total) end This is going to run 12 queries in order to get the […]