模型之间的关联问题RAILS 4

时间:2015-05-20 12:50:38

标签: ruby-on-rails

我的模特 汽车品牌模型

class CarBrand < ActiveRecord::Base
    has_many :car_ads
end

CAR ADVERTISEMENTS MODEL

class CarAd < ActiveRecord::Base
    has_one :car_brand
end

我的控制员:

    def index
        @car_ads = CarAd.all.order("car_ads.created_at DESC")
      end

汽车广告迁移:

    class CreateCarAds < ActiveRecord::Migration
  def up
    create_table :car_ads do |t|
      t.integer "user_id"
      t.integer "car_brand_id"
      t.integer "car_model_id"
      t.integer "state_id", :limit => 2
      t.integer "vin_id"
      t.integer "year_manufac", :precision => 4
      t.integer "km_age"
      t.integer "price_usd", :limit => 7      
      t.integer "car_tel_number", :precision => 8
      t.float "motor_volume", :limit => 10
      t.string "transmission"
      t.integer "horse_power", :limit => 3
      t.text "description"
      t.boolean "visible", :default => true      
      t.boolean "active", :default => true
      t.string "photo_file_name"
      t.string "photo_content_type"
      t.integer "photo_file_size"
      t.datetime "photo_updated_at"
      t.timestamps null: false
    end
    add_index :car_ads, :user_id
    add_index :car_ads, :car_brand_id
    add_index :car_ads, :car_model_id
    add_index :car_ads, :state_id
    add_index :car_ads, :vin_id
  end

  def down
    drop_table :car_ads
  end
end

汽车品牌迁移

    class CreateCarBrands < ActiveRecord::Migration
  def up
    create_table :car_brands do |t|
      t.string "brand", :limit => 20    
      t.timestamps null: false
    end
  end

  def down
    drop_table :car_brands
  end
end

所以问题是我无法获得汽车品牌形式的汽车广告,请帮忙, 我想要那样 迭代

<% @car_ads.each do |carad|%>
<%= carad.car_brand %>
<%end%>

2 个答案:

答案 0 :(得分:0)

修改CAR ADVERTISEMENTS MODEL

类CarAd&lt;的ActiveRecord ::基

SELECT
    comment,
    SUM(down_time)
FROM
(
    SELECT
        down_event.comment,
        TIMESTAMPDIFF(
            second,
            down_event.date_time,
            COALESCE(
                MIN(next_event.date_time),
                '2015-05-20 13:23:58'
            )
        )       AS down_time
    FROM
        event   down_event
    LEFT JOIN
        event   next_event
            ON  next_event.comment      =    down_event.comment
            AND next_event.date_time    >    down_event.date_time
            AND next_event.date_time BETWEEN '2015-05-20 00:00.00'
                                       AND   '2015-05-20 13:23:58'
    WHERE
            down_event.type         =    'DOWN'
        AND down_event.date_time BETWEEN '2015-05-20 00:00.00'
                                   AND   '2015-05-20 13:23:58'
    GROUP BY
        down_event.comment,
        down_event.date_time
)
    AS down_time
GROUP BY
    comment
;

修改您的控制器:

belongs_to :car_brand

答案 1 :(得分:0)

您没有在CarAd表中添加对CarBrand的任何引用,只需添加car_ad_id列并进行此类迁移

class AddCarBradIdToCarAd < ActiveRecord::Migration
  def change
    add_column :car_ads, :car_brand_id, :integer
  end
end

因此rails可以从CarAd获取相应的CarBrand