Use the where method to narrow down by referring to the value of another model.

Overview

Since we narrowed down multiple conditions using the where method, we will summarize them.

Thing you want to do

I want to get one piece of information about the developer model created just before the time when the product model was created.

Prerequisites

Create a Product model and a Developer. The two models are not associated. (Dare here) The Developer model has information about what product it made (developed_product_number). The product has a product number, and these two values are equal. (If so, let's associate it, but this time we will use this condition.) I want to get the value of the developer created just before the product is created from multiple developers who created the same product.

Implementation

Let's put the actual code first.

product = Product.find(params[:id])


@Developer = Developer.where(developed_product_number: product.id).where('created_at < ?', product).order(created_at: :desc).limit(1) 

It is like this. I will explain

product = Product.find(params[:id])

Get the ID of the target product and assign it to the variable product. Here, enter the value that was narrowed down by adding conditions to the variable @Developer.

@Developer = Developer.where(developed_product_number: product.id).where('created_at < ?', product).order(created_at: :desc).limit(1) 

Partially divide.

@Developer = Developer.where(developed_product_number: product.number)

Here, the id of the variable product obtained earlier is obtained.

Then narrow down the values.

where('created_at < ?', product).order(created_at: :desc).limit(1) 

Here, we acquired the information of multiple developers created before the time when the product was created, and acquired the information of the first developer counting from the descending order. It's been quite long, so the code here needs to be separated into scopes and so on.

Recommended Posts

Use the where method to narrow down by referring to the value of another model.
[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Rails] Don't use the select method just to narrow down the columns!
Output of how to use the slice method
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
I want to narrow down the display of docker ps
How to use the link_to method
How to use the include? method
How to use the form_with method
[Rails] Talk about paying attention to the return value of where
[Rails] How to use the map method
[Java] How to use the toString () method
How to pass the value to another screen
[Rails] Implementation procedure of the function to tag posts without gem + the function to narrow down and display posts by tags
When you want to use the method outside
How to use the replace () method (Java Silver)
[Ruby basics] How to use the slice method
Pass arguments to the method and receive the result of the operation as a return value
[Java] How to use compareTo method of Date class
I want to call a method of another class
I was addicted to the record of the associated model
[Java] How to get the maximum value of HashMap
[Rails] I don't know how to use the model ...
Java: Use Stream to sort the contents of the collection
I want to fetch another association of the parent model from the intermediate table with has_many
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to use the getter / setter method (in object orientation)
How to use trained model of tensorflow2.0 with Kotlin / Java
Use collection_select to pull down the data stored in Active_Hash
[Rails] Register by attribute of the same model using Devise
I want to expand the clickable part of the link_to method
You can solve the problem by referring to the two articles !!!
I want to use the sanitize method other than View.
How to change the setting value of Springboot Hikari CP
[Rails] How to display the list of posts by category
Extending the four-tier model of the architecture proposed by Eric Evans
Use WHERE to retrieve the result calculated using the Mysql syntax
Summary of values returned by the Spliterator characteristics method #java
[Rails] Where to be careful in the description of validation
Extract the uniquely identifying value of the table created by PostgreSQL
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot