找到你要的答案

Q:Active record Query for has many through relationship

Q:活动记录查询有许多通过关系

I have a has_many through relationship in my rails app and its associated as follows:

class Car < ActiveRecord::Base
     has_many :car_fuel_types
     has_many :fuel_types , through: :car_fuel_types 
end

class FuelType < ActiveRecord::Base
    has_many :car_fuel_types
    has_many :cars , through: :car_fuel_types
end

class CarFuelType < ActiveRecord::Base
    belongs_to :fuel_type
    belongs_to :car
end

In my app the user selects a fuel_type and on the basis of the selected fuel_type I have to filter the cars.

So, in the cars controller I have the fuel_type_id that is passed as a parameter by the user and now I need to select the cars corresponding to that fuel_type.

Can anybody help me in writing a active record query to do this ?

EDIT 1:

Incorporating the answer below,

I am using the following code in the index action of my cars controller:

def index   
  @cars= FuelType.find(params[:fuel_type_id]).cars

respond_to do |format|
  format.html
  format.json { render json: @cars }
end

end

I am using the following curl command to test it

 curl -i -H "Accept: application/json" -H "Content-type: application/json"  -H "X-User-Email: $EMAIL" -H "X-User-Token: $TOKEN" -X GET http://localhost:3000/cars.json\?id\=4

This is the query that is getting executed

  SELECT  "fuel_types".* FROM "fuel_types" WHERE "fuel_types"."id" = $1 LIMIT 1  [["id", 4]]

  Car Load (0.7ms)  SELECT "cars".* FROM "cars" INNER JOIN "car_fuel_types" ON "cars"."id" = "car_fuel_types"."car_id" WHERE "car_fuel_types"."fuel_type_id" = $1  [["fuel_type_id", 4]]

  Completed 500 Internal Server Error in 85ms (ActiveRecord: 17.0ms)

   NoMethodError - undefined method `fuel_type_id' for #<Car:0x007f273923eb70>:

Can anybody help me figure out why is this error happening ?

我有一个has_many通过我的Rails应用程序及其相关的如下关系:

class Car < ActiveRecord::Base
     has_many :car_fuel_types
     has_many :fuel_types , through: :car_fuel_types 
结束

class FuelType < ActiveRecord::Base
    has_many :car_fuel_types
    has_many :cars , through: :car_fuel_types
结束

class CarFuelType < ActiveRecord::Base
    belongs_to :fuel_type
    belongs_to :car
结束

在我的应用程序的用户选择fuel_type对选定的fuel_type我过滤车的基础。

所以,在车上我有fuel_type_id控制器,用户作为一个参数传递,现在我需要选择对应于fuel_type汽车。

有谁能帮我写一个活动记录查询来做这件事吗?

编辑1:

下面的答案,

我在我的汽车控制器的索引动作中使用以下代码:

def index   
  @cars= FuelType.find(params[:fuel_type_id]).cars

respond_to do |format|
  format.html
  format.json { r结束er json: @cars }
结束

结束

我使用下面的卷曲命令来测试它

 curl -i -H "Accept: application/json" -H "Content-type: application/json"  -H "X-User-Email: $EMAIL" -H "X-User-Token: $TOKEN" -X GET http://localhost:3000/cars.json\?id\=4

这是正在执行的查询

  SELECT  "fuel_types".* FROM "fuel_types" WHERE "fuel_types"."id" = $1 LIMIT 1  [["id", 4]]

  Car Load (0.7ms)  SELECT "cars".* FROM "cars" INNER JOIN "car_fuel_types" ON "cars"."id" = "car_fuel_types"."car_id" WHERE "car_fuel_types"."fuel_type_id" = $1  [["fuel_type_id", 4]]

  Completed 500 Internal Server Error in 85ms (ActiveRecord: 17.0ms)

   NoMethodError - undefined method `fuel_type_id' for #<Car:0x007f273923eb70>:

有谁能帮我弄明白为什么会有这样的错误发生?

answer1: 回答1:

This should probably return what you need:

FuelType.find(fuel_type_id).cars

这可能会回报你所需要的:

FuelType.find(fuel_type_id).cars
ruby-on-rails  activerecord  has-many-through