天道酬勤,学无止境

ruby-on-rails

Rails ActiveRecord: Missing column in grouping query

Question Key.select('products.name as product, product_groups.name as product_group, AVG(keys.cost) as cost') .group('products.id, product_groups.id') .left_joins(:product,:product_group) the result: => #<ActiveRecord::Relation [#<Key id: nil, cost: 0.6e1>, #<Key id: nil, cost: 0.4e1>]> Expected return 3 field, but returnig value: 2 field. I found the solution. The detail areas in the console did not appear as HASH. Answer1 In my understanding the grouping statement would only return Aggregated columns and columns that are used to group the data set. In your case you have not used the grouping

2022-07-11 09:59:46    分类:技术分享    ruby-on-rails   ruby   activerecord

Rails ActionCable Connection(Server) with NodeJs(Client)

Question I want to create a connection between rails ActionCable which will act as Server and NodeJs as Client. This is my code in connection.rb file. # app/channels/application_cable/connection.rb module ApplicationCable class Connection < ActionCable::Connection::Base identified_by :uuid def connect self.uuid = SecureRandom.uuid end end end This is code of my channel. # app/channels/socket_connection_channel.rb class SocketConnectionChannel < ApplicationCable::Channel def subscribed stream_from "socket_connect" end def speak ActionCable.server.broadcast("socket_connect", message: "Connected"

2022-05-17 04:28:04    分类:技术分享    ruby-on-rails   node.js   websocket   client-server   actioncable

Rails 3/Devise confirmation email server settings

Question I have a Rails 3 app and I'm using Devise for user authentication. How do I specify an application-wide SMTP server for Devise to use? I've entered the following into /config/environments/development.rb (Apache2 is set to 8080) config.action_mailer.default_url_options = { :host => 'mydomain.com:8080' } Any ideas on how to get outgoing mail to work with Devise and Rails 3? Answer1 Devise uses ActionMailer to send emails so you have to configure it. Take a look at this tutorial for an example of setting up ActionMailer. Also if you are using GMail take a look at this question for

2022-05-17 03:38:15    分类:技术分享    ruby-on-rails   devise   actionmailer

Ruby on Rails: Why a select box does not show the current object value?

Question Here is the relevant code from views/products/edit.html.erb: <%= form_for(:product, :url => {:action => 'update', :id => @product.id}) do |f| %> <%= render(:partial => "form", :locals => {:f => f}) %> <%= submit_tag("Update Product") %> <% end %> from views/products/_form.html.erb: <%= select_with_new_option(f, :shop, :name, :id) %> and from helpers/products_helper.rb: def select_options_with_create_new(objects, text_field, value_field, options={}) object = objects.to_s.singularize all_objects = object.capitalize.constantize.all all_objects = all_objects.sort_by(&options[:order_by])

2022-05-17 03:01:47    分类:技术分享    ruby-on-rails   forms   select   ruby-on-rails-3

Advice on how to send email to multiple users based on an association

Question I have created a Ruby on Rails application where users can track workouts. The can do so either privately or publicly. On workouts which are public ( workout.share == 1 ) I allow users to comment. When a comment is created on a workout, the workout owner is notified via email. That all works great. I am now looking for some advice on the best way to allow users who have commented on a workout, to also be notified via email. Here is an example. User A creates Workout 1. User B comments on Workout 1 and User A receives an email notification. User C also comments on Workout 1 and both

2022-05-17 02:32:04    分类:技术分享    ruby-on-rails   ruby   email   mailer

rails redirect_to :back not working

Question I'm trying to use the following: class PaymentsController < ApplicationController def addproduct (session[:products] ||= []) << params[:item] redirect_to :back end end I got this exception: undefined method `back_url' for #<PaymentsController:0x007ff682c467a8> Why this is happening? Answer1 Rails 5 has redirect_back, instead of redirect_to :back. It was changed as it used to raise an exception when request's HTTP_REFERER was not present. So use this: redirect_back fallback_location: root_path You can change root_path to something else as per your requirements. Answer2 redirect_to

2022-05-17 00:56:04    分类:技术分享    ruby-on-rails   ruby   redirect

Collection_select not saving

Question I'm trying to implement a collection select to create associations between my model group and user (join table: groups_user). But this association is not saving when i'm using a collection_select. And I have absolutely no idea what can be the problem My code : view : <%= simple_form_for(group, remote: true) do |f| %> <%= f.collection_select :user_ids, @users.order(:pseudo), :id, :pseudo, {}, {multiple: true} %> <%= f.submit %> <% end %> & my controller(group) : def group_params params.require(:group).permit(:name, :user_ids) end Just for info, i have a action to submit individually

2022-05-16 23:38:19    分类:技术分享    ruby-on-rails   ruby   collection-select

Ruby strftime '%Z' method returns '0545' instead of 'NPT'

Question After upgrading my MacOS to latest version, I am having some weird issue with the Time#strftime method. Time.now.in_time_zone("Kathmandu").strftime("%Z") #=> '+0545' Time.now.in_time_zone("Bangkok").strftime("%Z") #=> '+07' Time.now.in_time_zone("Nairobi").strftime("%Z") #=> 'EAT' Time.now.in_time_zone("New Delhi").strftime("%Z") #=> 'IST' My current ruby version is: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16] I have tried it on: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16] and still not working I tried this on my friend's machine (ruby 2.4.1p111 (2017

2022-05-16 19:06:12    分类:技术分享    ruby-on-rails   ruby   macos

Rails Perform Tasks Async In Controller

Question I have three tasks that I need to do when a route in my Controller is called. Right now my code looks something like this (abbreviated): def set_quotas TaskOne.new().ex TaskTwo.new().ex TaskThree.new().ex quotas = @user.quotas render json: quotas, status: 200, each_serializer: Api::V1::QuotaSerializer end Each task runs in order. However, several of them call to outside services. So the total time it takes to complete this call ends up being 4-8 seconds and we really want to speed that up. What I'd like to do is run all three tasks in tandem, but wait until each is complete before I

2022-05-16 12:16:03    分类:技术分享    ruby-on-rails

What can I use to allow an android app to communicate with a rails app

Question I'm trying to create an application that basically lets a user on an android device send a message that instantly appears in the rails application. Does anyone have suggestions to what I would use to do this? Answer1 Here is a fast walkthrough. Preparing the base application Let's create a new rails app $ rails new simple-message $ cd simple-message/ We are now going to create a RESTful resource called Message, that will manage the messages coming from your mobiles. $ rails generate scaffold Message title:string content:text Create the table that will contain the messages: $ rake db

2022-05-16 12:09:08    分类:技术分享    android   ruby-on-rails