Rails – Order index by complex sort

How do I order my index results by featured_end_date >= Time.now() in :asc order and then have the rest of the results sort by by publish_at: :desc.

Currently I have BlogPost.order(featured_end_date: :asc, publish_at: :desc). I am missing that >= Time.now() comparison.

I assume scopes might need to be used, but I am not sure how to achieve this.

Ruby on Rails has_many association define custom ordering

I have run into a problem and can’t figure out how to fix it.

I basically have a ModelA that has a has_many relationship with another ModelB. Let’s say that ModelB has two attributes. A created_at and a filename. Now let’s say the created_at is not actually accurate due to a race condition. However the filename contains a timestamp that is accurate. Unfortunately the filename is also inconsistent and extracting and adding the timestamp beforehand is not an option.

Is there any way to do something like this?

class ModelA
    has_many: model_bs, order: extracted_timestamp_from_filename.asc

    def extracted_timestamp_from_filename
        #self would ideally be a single model_bs

At least with order I don’t think this could ever work without having an actual proper timestamp attribute. Is there a way to get this functionality? If there is a way to do this with hooks somehow I haven’t found it.

How do I display dynamic fields from collection and bind data back in ruby on rails?

I just started working on Ruby on Rails project for maintenance. I have array of objects and want to display separate section of fields for each object. They have design to call field factory that generate field dynamically. I could display the data but it not binding the value back on page submission

ClassRoom Controller

def arrayOfStudents
  @arrayOfStudents||= begin
    if Array(attributes[:arrayOfStudents]).any?
      attributes[:arrayOfStudents]   ==> this always have 5 objects of Students with variables name and age

My view

<% @classroom.arrayOfStudents.each do |crt| %>
  <%= f.fields_for crt do |ff| %>
    <%= render 'shared/model_attributes/field', f: ff, field: {type: :money, cols: 6, name: :name, label: 'Name'}, static: static %>
    <%= render 'shared/model_attributes/field', f: ff, field: {type: :percent, cols: 6, name: :age, label: 'Age'}, static: static %>
    <%= puts "testing"%>
  <% end %>
<% end %>


<% when :money %>
    <%= f.labeled_text_field_with_begin_addon(field[:name], '$', label: field[:label], class: class_list, placeholder: field[:label], maxlength: field[:maxlength], display_name: false, disabled: false, reserve_error_space: true, help_text: field[:help_text]) %>

After editing the value and submit control comes to controller. But the values inside :arrayOfStudents are unchanged . Am I missing generating dynamic fields correctly?

Return Processed Data From The Fastest Thread Ruby

I want to hit a URL which will eventually return the required data. Sometimes the requests timeout & it took upto 1 minute to receive the data.
I want to make it as fast as possible. I am thinking of starting multiple threads & use the data from the earliest completed thread. Can anyone please help with the best approach?

I think I can do it via a infinite loop waiting for the result in an array from the thread but that seems to be a very inefficient way of doing it.

Handling join table entries based on association attributes


What is the best way to create join table entries based on a form with the attributes of a association, like a bar code or a plate number?

Detailed explanation

In this system that records movements of items between storage places, there is a has_many_and_belongs_to_many relationship between storage_movements and storage_items because items can be moved multiple times and multiple items can be moved at once.

These items are previously created and are identified by a plate number that is physically attached to the item and recorded on its creation on the application.

The problem is that I need to create storage_movements with a form where the user inputs only the plate number of the storage_item that is being moved but I cant figure it out a way to easily do this.

I have been hitting my head against this wall for some time and the only solution that I can think of is creating nested fields on the new stock_movements form for the stock_items and use specific code on the model to create, update and delete these stock_movements by explicitly querying these plate numbers and manipulating the join table entries for these actions.

Is this the correct way of handling the problem? The main issue with this solution is that I can’t seem to display validation errors on the specific plates number that are wrong (I’m using simple_forms) because I don’t have storage_item objects to add errors.

Below there is a snipped of the code for the form that I’m currently using. Any help is welcome 😀

<%= simple_form_for @storage_movement do |movement_form| %>
  #Other form inputs
  <%= movement_form.simple_fields_for :storage_items do |item_form| %>
    <%= item_form.input :plate, label: "Plate number" %>
  <% end %>
<% end %>

Unpermitted parameters for form_with(:url) in Rails 5

I have:

<%= form_with url: retrieve_videos_path, local: true do |form| %>
   # some form stuff
   <%= form.submit %>
<% end %>

And when I submit it, logs signal about unpermitted parameters: Unpermitted parameters: :utf8, :authenticity_token, :commit

I guess, in forms for models these parameters are permitted by the ‘require’ method like:


But how to make the same for urls?

GKE kubernetes delayed_job pod logs

I have a deployment object with the following rake jobs:work command:

apiVersion: extensions/v1beta1
kind: Deployment
  name: staging-delayed-job-deployment
  namespace: staging
        env: staging
        name: delayed-job
        - name: job
          image: gcr.io/ej-gc-dev/redacted:<%= ENV['IMAGE_TAG'] %>
          command: ["/bin/bash", "-l", "-c"]
          args: ["RAILS_ENV=production bundle exec rake jobs:work"]

When I run kubectl logs I get nothing. How do I get the rake jobs:work output to show in kubectl logs ? i.e if run directly in the pod it gives output like this:

[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Starting job worker
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job UpdateHubspotPersonaJob (id=67) RUNNING
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job UpdateHubspotPersonaJob (id=67) COMPLETED after 0.4903
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job Mailerjack.registration_created_user_welcome (id=68) RUNNING
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job Mailerjack.registration_created_user_welcome (id=68) COMPLETED after 0.9115
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job UpdateHubspotPersonaJob (id=69) RUNNING
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job UpdateHubspotPersonaJob (id=69) COMPLETED after 0.1752
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job Mailerjack.registration_created_user_welcome (id=70) RUNNING
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] Job Mailerjack.registration_created_user_welcome (id=70) COMPLETED after 0.4770
[Worker(host:staging-deployment-cc7dc559f-bswvr pid:2381)] 4 jobs processed at 1.7649 j/s, 0 failed

I want that to show up when I run kubectl logs