ruby-on-rails-4

Incorporating Custom SELECT clause in ActiveRecord query

早过忘川 提交于 2020-02-03 02:05:48
问题 I have a large table of assessments, in that table there are several integer columns with numerical scores. I am trying to remove some N+1 iteration to speed up my application. I can generate this query in raw SQL to gather all of the scores query = 'SELECT ' + Assessment.rating_attributes.collect{|attribute| 1.upto(5).collect do |i| %Q{SUM(CASE WHEN #{attribute.to_s} = #{i} then 1 else 0 end) as #{attribute}_score_#{i}} end }.join(', ') + ' FROM assessments' When I execute this query with: $

Rails: can't pass variable to partial, what am I doing wrong?

一笑奈何 提交于 2020-02-02 16:07:16
问题 I'm trying a few different ways to define and pass the local variable to the partial, but it keeps saying it's undefined: in Show file: <% @startups.each do |startup| %> <%= render :partial => "profile/startup" %> <% end %> in partial: <%= simple_form_for [@commentable, @comment], :remote => true do |form| %> <%= form.input :content, label: false, :input_html => { :id => "#{startup.user_id}" } %> <%= form.submit "Submit" %> <% end %> These are the other ways I'm trying to pass the variable,

Rails: can't pass variable to partial, what am I doing wrong?

扶醉桌前 提交于 2020-02-02 16:02:23
问题 I'm trying a few different ways to define and pass the local variable to the partial, but it keeps saying it's undefined: in Show file: <% @startups.each do |startup| %> <%= render :partial => "profile/startup" %> <% end %> in partial: <%= simple_form_for [@commentable, @comment], :remote => true do |form| %> <%= form.input :content, label: false, :input_html => { :id => "#{startup.user_id}" } %> <%= form.submit "Submit" %> <% end %> These are the other ways I'm trying to pass the variable,

Rails: can't pass variable to partial, what am I doing wrong?

ⅰ亾dé卋堺 提交于 2020-02-02 16:00:06
问题 I'm trying a few different ways to define and pass the local variable to the partial, but it keeps saying it's undefined: in Show file: <% @startups.each do |startup| %> <%= render :partial => "profile/startup" %> <% end %> in partial: <%= simple_form_for [@commentable, @comment], :remote => true do |form| %> <%= form.input :content, label: false, :input_html => { :id => "#{startup.user_id}" } %> <%= form.submit "Submit" %> <% end %> These are the other ways I'm trying to pass the variable,

Rails 4 with MailCatcher and Devise gems

前提是你 提交于 2020-02-02 06:50:46
问题 I have an app with gem Devise. I need to confirm registration by email. In config/environment.rb I added this ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :domain => "gmail.com", :authentication => 'plain', :user_name => "myappname@gmail.com", :password => "mypassword" } In config/environment/development.rb added: config.action_mailer.default_url_options = { :host => 'localhost:3000' } config.action_mailer.delivery

Convert Array#select to active record query in rails 4

拥有回忆 提交于 2020-02-01 07:49:22
问题 I'm writing a custom search function, and I have to filter through an association. I have 2 active record backed models, cards and colors with a has_many_and_belongs_to, and colors have an attribute color_name As my DB has grown to around 10k cards, my search function gets exceptionally slow because i have a select statement with a query inside it, so essentially im having to make thousands of queries. i need to convert the array#select method into an active record query, that will yield the

Rails 4 - Devise: getting ActionController::UnknownFormat on signup

醉酒当歌 提交于 2020-02-01 03:48:10
问题 I have a Rails 4.2.3 app where I use Devise for user authentication. I present my signup form in a Bootstrap modal. I have implemented it similar to: https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app. On signup I keep getting this error: Completed 406 Not Acceptable in 512033ms (ActiveRecord: 5.8ms) ActionController::UnknownFormat (ActionController::UnknownFormat) And I'm not sur how to fix it. I use custom controllers for sessions and

Route concern and polymorphic model: how to share controller and views?

僤鯓⒐⒋嵵緔 提交于 2020-01-30 18:48:47
问题 Given the routes: Example::Application.routes.draw do concern :commentable do resources :comments end resources :articles, concerns: :commentable resources :forums do resources :forum_topics, concerns: :commentable end end And the model: class Comment < ActiveRecord::Base belongs_to :commentable, polymorphic: true end When I edit or add a comment, I need to go back to the "commentable" object. I have the following issues, though: 1) The redirect_to in the comments_controller.rb would be

CollectionProxy vs AssociationRelation

白昼怎懂夜的黑 提交于 2020-01-29 04:33:54
问题 I am wondering about the difference between ActiveRecord::Associations::CollectionProxy and ActiveRecord::AssociationRelation . class Vehicle < ActiveRecord::Base has_many :wheels end class Wheel < ActiveRecord::Base belongs_to :vehicle end So if I do: v = Vehicle.new v.wheels # => #<ActiveRecord::Associations::CollectionProxy []> v.wheels.all # => #<ActiveRecord::AssociationRelation []> I have no idea what is the difference between them and why this is implemented this way? 回答1: ActiveRecord

CollectionProxy vs AssociationRelation

非 Y 不嫁゛ 提交于 2020-01-29 04:32:26
问题 I am wondering about the difference between ActiveRecord::Associations::CollectionProxy and ActiveRecord::AssociationRelation . class Vehicle < ActiveRecord::Base has_many :wheels end class Wheel < ActiveRecord::Base belongs_to :vehicle end So if I do: v = Vehicle.new v.wheels # => #<ActiveRecord::Associations::CollectionProxy []> v.wheels.all # => #<ActiveRecord::AssociationRelation []> I have no idea what is the difference between them and why this is implemented this way? 回答1: ActiveRecord