ruby-on-rails-4.1

Negate ActiveRecord query scope

强颜欢笑 提交于 2019-12-05 02:42:46
I have a slightly complicated scope on a model class Contact < ActiveRecord::Base scope :active, -> { where(inactive: false) } scope :groups, -> { where(contact_type: 2308) } scope :group_search, -> (query) do active.groups.where("last_name LIKE '%' + ? + '%'", query) end end For testing purposes, I want to make sure that all Contacts not returned by group_search are excluded for the right reasons. But to get that list, I have to load Contact.all - Contact.group_search('query') which runs two queries, returns an Array instead of a Relation , and is slower than I'd like. And since I'm testing

Rails 4 Sum by Model Method

ε祈祈猫儿з 提交于 2019-12-04 16:23:55
问题 In my app, I have a User model, with a goal_ytd method, which performs some calculations. In a controller, I have a variable @users that might be User or an ActiveRecord::Relation of users , and I would like to sum all of the @users 's goal_ytd s. My first inclination was: @users.sum(&:goal_ytd) Which threw a deprecation warning in both cases because using sum on an ActiveRecord::Relation is going away in Rails 4.1. So, I changed the code to: @users.to_a.sum(&:goal_ytd) Which then threw a

Rails 4.1.1 w/ pg_search - “PG::SyntaxError: ERROR: syntax error at or near ”AS“” Bug

不打扰是莪最后的温柔 提交于 2019-12-04 12:37:20
问题 I updated from RoR 4.0.4 to 4.1.1 to apply the latest security patch and it appears pg_search broke. Here's a error: PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1: ...sh', ''' ' || unaccent('banner') || ' ''')), 0))) AS pg_sear... I'm searching via ajax but i don't think that is the issue based on the output above. I'm also using will_paginate but the branch I'm using is supposed to fix previous issues with rails 4.1.x and pg_search. gem 'will_paginate', :git => 'https://github

Very Basic Rails 4.1 API Call using HTTParty

依然范特西╮ 提交于 2019-12-04 07:25:37
Relatively new to Rails. I am trying to call an API and it's supposed to return a unique URL to me. I have HTTParty bundled on my app. I have created a UniqueNumber controller and I have read through several HTTParty guides as far as what I want but maybe I'm just a bit lost and really have no idea what to do. Basically, all I need to do is call the API, get the URL it returns, then insert that URL into the database for a user. Can anyone point me in the right direction or share some code with me? Let's assume the API is in a JSON format and returns the data like so: {"url": "http://example

Rails 4 add new column or field in model

半腔热情 提交于 2019-12-04 03:44:14
问题 I try to add new column into existing model in rubyonrails. How can I add single or multiple field into my existing model. 回答1: if you want to add a new column then run rails generator with Add[column]To[model] format followed by column $ rails g migration AddBioToNameOfModels bio:text invoke active_record create db/migrate/YYYYMMDDHHMMSS_add_bio_to_name_of_models.rb then run rake db:migrate 来源: https://stackoverflow.com/questions/30528877/rails-4-add-new-column-or-field-in-model

Rails 4.1 AWS Beanstalk cannot find secret key base

跟風遠走 提交于 2019-12-03 17:16:58
I am trying to upload my rails project on AWS Beanstalk. I've already run eb init, eb start and configured the database settings to point to RDS. After I pushed using git aws.push and waited for AWS server to be started, the link provided says: "502 Bad Gateway nginx" In the logs ------------------------------------- /var/app/support/logs/passenger.log ------------------------------------- App 6861 stderr: [ 2014-05-29 13:26:59.1308 6893/0x00000001e50050(Worker 1) utils.rb:68 ]: *** Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set

Rails 4.1.1 w/ pg_search - “PG::SyntaxError: ERROR: syntax error at or near ”AS“” Bug

亡梦爱人 提交于 2019-12-03 08:20:43
I updated from RoR 4.0.4 to 4.1.1 to apply the latest security patch and it appears pg_search broke. Here's a error: PG::SyntaxError: ERROR: syntax error at or near "AS" LINE 1: ...sh', ''' ' || unaccent('banner') || ' ''')), 0))) AS pg_sear... I'm searching via ajax but i don't think that is the issue based on the output above. I'm also using will_paginate but the branch I'm using is supposed to fix previous issues with rails 4.1.x and pg_search. gem 'will_paginate', :git => 'https://github.com/nazgum/will_paginate.git' #until rails 4.1 will_paginate fixed This is the code model where i'm

Ruby 2.1 with erubis Template Engine

你。 提交于 2019-12-01 12:27:27
We are looking for fastest template engine for rendering of views. As i understand erubis is the fastest template engine in ruby. My usecase is render templates through script. Looking at the gem official page it's latest release was in 2011. Not sure if the community is active. https://rubygems.org/gems/erubis/versions Does anyone use ruby 2.1 with erubis template engine? Is it recommended to use erubis with ruby 2.1? Thanks Abhay abrocks I ran benchmark between ERB and erubis rendering with below code snippet. erubis_render_time = Benchmark.realtime { template_content = File.read("#{Rails

Ruby 2.1 with erubis Template Engine

一笑奈何 提交于 2019-12-01 11:14:52
问题 We are looking for fastest template engine for rendering of views. As i understand erubis is the fastest template engine in ruby. My usecase is render templates through script. Looking at the gem official page it's latest release was in 2011. Not sure if the community is active. https://rubygems.org/gems/erubis/versions Does anyone use ruby 2.1 with erubis template engine? Is it recommended to use erubis with ruby 2.1? Thanks Abhay 回答1: I ran benchmark between ERB and erubis rendering with

validates_inclusion_of no longer working the same in Rails 4.1?

梦想与她 提交于 2019-11-30 09:30:19
The following code made sure that a time_zone chose is within the time zones in ActiveSupport::TimeZone.us_zones : validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name) Worked great in Rails 4.0. Just upgraded to Rails 4.1 and I'm getting this error on my index page (so just simply viewing the models): An object with the method #include? or a proc, lambda or symbol is required, and must be supplied as the :in (or :within) option of the configuration hash I'm guessing from that, ActiveSupport::TimeZone.zones_map(&:name) is no longer a valid value for the in property?