我的模特汽车品牌模特
class CarBrand < ActiveRecord::Base
has_many :car_ads
end
汽车广告模型
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%>
修改汽车广告模型
CarAd类<ActiveRecord :: Base
belongs_to :car_brand
结尾
修改您的控制器:
def index
@car_ads = CarAd.all.order("created_at DESC")
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句