ActiveRecord的寻找,只返回选定列对准[:ID] [英] ActiveRecord find and only return selected columns aligned with [:id]

查看:103
本文介绍了ActiveRecord的寻找,只返回选定列对准[:ID]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据,是在两个不同的表。我想加入他们,在我的/显示网页在我的Ruby on Rails应用程序。在控制器中,我有它设置为找到为止,设置一个变量,使用日期去翻其他数据库拉所需要的信息。

I have a set of data that is in two different tables. I want to join them up on my /show page in my ruby on rails app. In the controller, I have it set to find the date, set a variable, use that date to look through the other database to pull the needed information.

我的控制器看起来是这样的:

My controller looks like this:

  def show
    @ticket = Ticket.find(params[:id])
    @hellodate = Ticket.select(:date)
    @winnings = Winnings.find(:all, :conditions => {:date => @hellodate})

  respond_to do |format|
   format.html # show.html.erb
   format.json { render json: @ticket }
   end
 end

我@winnings工作,我需要它来拉,有与@ticket日期匹配的日期的行。我是新来这个世界,会爱任何输入/解决方案,因为这是行不通的。它应该只展示一个@winnings,但它显示了多个尽管只有一个日期将前夕比赛。谢谢!

for my @winnings to work, I need it to pull the row that has the date that matches with the @ticket date. I am new to this world and would love any input / solutions because this isn't working. It should only show one @winnings but it shows multiple though only ONE date will eve match. Thanks!

推荐答案

@hellodate 不是你认为它是。这:

Your @hellodate is not what you think it is. This:

@hellodate = Ticket.select(:date)

会,或多或少,给你说的结果是:

will, more or less, give you the result of saying:

select "date" from "tickets"

所以你会得到所有的门票取值但是只有日期列将被拉出的数据库。 presumably你只是想在日期 @ticket

so you'll get all Tickets but only the date columns will be pulled out of the database. Presumably you just want the date from @ticket:

@ticket   = Ticket.find(params[:id])
@winnings = Winnings.where(:date => @ticket.date)

这篇关于ActiveRecord的寻找,只返回选定列对准[:ID]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆