简体   繁体   English

从关联表Ruby on Rails中获取第一项

[英]Get first entry from an associated table Ruby on Rails

I have a one to many relationship: User has many Payments. 我有一对多的关系:用户有很多付款。 I am trying to find a query that gets the first payment of each user(using created_at from the payments table). 我试图找到一个查询,该查询获取每个用户的第一笔付款(使用来自付款表的created_at)。

I have found a similar question with an SQL response, but I have no idea how to write it with Active Record. 我在SQL响应中发现了类似的问题,但是我不知道如何使用Active Record编写它。

how do I query sql for a latest record date for each user 如何查询sql以获取每个用户的最新记录日期

Quoting the answer: 引用答案:

select t.username, t.date, t.value
from MyTable t
inner join (
    select username, max(date) as MaxDate
    from MyTable
    group by username
) tm on t.username = tm.username and t.date = tm.MaxDate

For me, it would be min instead of max. 对我来说,这将是最小值而不是最大值。

Thank you :) 谢谢 :)

Try this one for POSTGRES 试试POSTGRES

Payment.select("DISTINCT ON(user_id) *").order("user_id, created_at ASC")

And For SQL 对于SQL

Payment.group(:user_id).having('created_at = MAX(created_at)')

If I'm going to answer the question above with: (I don't based on given raw SQL ) 如果我要通过以下方式回答上述问题:(我不基于给定的原始SQL

User has many Payments. 用户有很多付款。 I am trying to find a query that gets the first payment of each user(using created_at from the payments table). 我试图找到一个查询,该查询获取每个用户的第一笔付款(使用来自付款表的created_at)。

Let say:
# Assumed to have a Single User, as reference
user = User.first

# Now, get first payment (from Payment model)
user.payments.last
# .last since it will always get the first created row by created_at.

If I fully understand what you're trying to do. 如果我完全了解您要做什么。 I'm don't know why you need max or min date ? 我不知道您为什么需要最大或最小日期

What about this? 那这个呢?

If you want first payment of each user 如果您想要每个用户的第一笔付款

dates = Payment.group(:user_id).minimum(:created_at).values
payments = Payment.where(created_at: dates)

From payment you can find user too. 通过付款,您也可以找到用户。

I think you have username as foreign key, you can change accordingly. 我认为您将用户名作为外键,可以进行相应更改。 :) Let me know if you face any issue, as I tested it works. :)让我知道您是否遇到任何问题,因为我进行了测试。

I know this answer is not the best, but it will work even or transactions with milliseconds difference, as rails saves date(created_at and updated_at) with ms level. 我知道这个答案不是最好的,但它甚至可以工作或以毫秒为单位进行交易,因为rails以ms级别保存日期(created_at和updated_at)。

I am sorry for not replying to everything, but after multiple tests, this is the quickest answer (in run time) I came with: 很抱歉没有回复所有内容,但是经过多次测试,这是我运行时最快的答案(在运行时):

Payment.where(:id => Payment.group(:user_id).pluck(:id))

I am saying it might not be the quickest way because I am using a sub query. 我说这可能不是最快的方法,因为我正在使用子查询。 I am getting the unique values and getting the ID's: 我正在获取唯一值并获得ID:

Payment.group(:user_id).pluck(:id)

Then I am matching those ID's. 然后我匹配那些ID。

The downside of this is that it won't work reversed, for getting the last payment. 不利的一面是,为了获得最后一笔付款,它不会逆转。

There was also a possibility to use group_by and map but, since map is coming from ruby, it is taking much more time. 也可以使用group_bymap但是由于map来自红宝石,因此需要花费更多时间。

I'm not sure but try this : 我不确定,请尝试以下操作:

In your controller : 在您的控制器中:

  def Page
    @payments = Payment.first
  end

in your html.erb : 在您的html.erb中:

<% @payments.each do |payment| %>
  <p> <%= payment.amount %> </p>

Hope this help ! 希望对您有所帮助!

Record.association.order(:created_at).first

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM