Asynchronous API call to update model in Rails

I am making a simple API call to Coinbase's API to get the current buy/sell price of bitcoin. I have a before_save action on my Price model class PriceBtc < ApplicationRecord before_save :save_prices def save_prices = PriceBtc.get_...
more »

2017-12-03 21:12 (1) Answers

Unable to run ActiveRecord Migration

I added the gemfile: gem 'activerecord-reputation-system', require: 'reputation_system' to my Rails app in order to add upvote/downvote functionity to a model (I'm following Railscast #364). I installed the gem with no problems. I entered 'rails ...
more »

2017-11-29 13:11 (1) Answers

Ruby: What does binds mean in ActiveRecord gem?

I saw binds as argument in lots of methods, but without any documentation. e.g. Rails Source code def find_by_sql(sql, binds = [], preparable: nil, &block) result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds, preparab...
more »

2017-11-29 07:11 (1) Answers

Ruby, query active model dynamically

I have an Order model in my ruby on rails application which has several attributes. I want to query those orders according to parameters given by users such as itemCount, totalPrice and created date. If one of those attributes is given, I mean it's ...
more »

2017-11-28 23:11 (1) Answers

Ruby aggregate function with model

I have a product model in my ruby on rails application which has name,endorsement and expense attributes. I need to write a query that list all records, but for every record I need to calculate endorsement-expense as income value. That seems to be o...
more »

2017-11-27 18:11 (3) Answers

Get hash of id => property in Rails

I have an ActiveRecord model like this: class Person < ActiveRecord::Base attr_accessible :name end and need to get a hash mapping Person's ids to their names: {1 => "Paul", 2 => "Aliyah", 3 => ... } Now, the obvious way would be ...
more »

2017-10-11 21:10 (3) Answers

Computing ActiveRecord nil attributes

In my Rails app I have something like this in one of the models def self.calc columns_to_sum = "sum(price_before + price_after) as price" where('product.created_at >= ?', 1.month.ago.beginning_of_day).select(columns_to_sum) end For some of ...
more »

2017-10-01 15:10 (2) Answers

Search field with autofilling multiple strings

I'm trying to do a pretty not standard search form for my recipes application. What it needs to do ? I need to have one text field which will can accept multiple search conditions in list manner. Similar thing that is here :
more »

2017-09-24 20:09 (0) Answers

Rails 5: can't order after group and count

In my subscribers table I have duplicate emails, so I want to count these. I'm doing this in the Rails console:"email, count(*)").group(:email).order("count (*) desc") This results in the following query to my PostgreSQL db: SEL...
more »

2017-09-19 15:09 (1) Answers

Rails pagination - faster count of total pages?

I'm using the Kaminari gem to paginate my searches: @outfits = Outfit.where("description LIKE ?", "%warm%").page(params[:page]).per(20) And using this code to decide whether or not to show more: <% unless @outfits.current_page == @outfits.tota...
more »

2017-09-18 19:09 (2) Answers

ActiveRecord query interface and map

I'm trying to map an AR relation in a specific data structure (to be rendered as JSON) and I can't make it work, for some reason the relations are always nil Client.includes(:fixed_odds_account, person: [:phones, :emails]).map do |client| { id...
more »

2017-09-13 18:09 (1) Answers

Create multiple records at once ruby on rails

This seemed fairly straightforward, but is proving not to be. I have two models: Authors and Books. Authors has_many books, books belong_to authors. I'd like to be able to add many books in a single form with different authors and information for ...
more »

2017-09-11 23:09 (1) Answers