0

私がやりたいとしましょう

SELECT persons.name, cars.registration FROM persons, cars 
   WHERE persons.state=cars.state AND persons.state_id_number=cars.owner_id_number ;

ヒント:異なる州の人々は同じものを持つことができますがstate_id_number、それは州内でユニークです。

できます

People.find_by_sql("SELECT persons.name, cars.registration FROM persons, cars
   WHERE persons.state=cars.state AND persons.state_id_number=cars.owner_id_number")

レコードのリストを取得します。

しかしfind(:all, :conditions => {、同じことをするためにフォーマットを使用できますか?

4

2 に答える 2

1

あなたが協会を持っていると仮定して:車のセットアップ

People.joins(:cars).where("persons.state=cars.state AND persons.state_id_number=cars.owner_id_number").all

まったく同じではありませんが、近いはずです

于 2012-10-12T22:03:03.740 に答える
0
class Person < ActiveRecord::Base
  has_many :cars,
      :foreign_key => "owner_id_number",
      :primary_key => "state_id_number",
      :conditions => "persons.state = cars.state"
end

class Car < ActiveRecord::Base
  belongs_to :person,
      :foreign_key => "owner_id_number",
      :primary_key => "state_id_number",
      :conditions => "persons.state = cars.state"
end

Person.includes(:cars)
于 2012-10-12T22:25:52.920 に答える