我有一个Roast
模型,其中有许多Countries
,其中有许多Regions
。
我正在努力让这些国家和地区能够在我的烤肉节目中展示。我可以看到我的Roast模型中有country_id和region_id的值,所以我知道已经插入了值。
在目前的形式中,它并没有真正抛出任何错误,所以我有点迷失下一步该尝试的内容?
从日志中看起来它似乎在尝试查找国家/地区值,但我无法看到区域:
Processing by RoastsController#show as HTML
Parameters: {"id"=>"belong_to"}
Roast Load (2.3ms) SELECT "roasts".* FROM "roasts" WHERE "roasts"."slug" = $1 LIMIT $2 [["slug", "belong_to"], ["LIMIT", 1]]
CACHE Roast Load (0.0ms) SELECT "roasts".* FROM "roasts" WHERE "roasts"."slug" = $1 LIMIT $2 [["slug", "belong_to"], ["LIMIT", 1]]
Roaster Load (0.4ms) SELECT "roasters".* FROM "roasters" WHERE "roasters"."roaster_id" = $1 LIMIT $2 [["roaster_id", 3], ["LIMIT", 1]]
Rendering roasts/show.html.erb within layouts/application
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."user_id" = $1 ORDER BY "users"."user_id" ASC LIMIT $2 [["user_id", 15], ["LIMIT", 1]]
Rendered users/shared/_links.html.erb (1.5ms)
Rendered partials/_mainnav.html.erb (15.6ms)
Country Load (0.4ms) SELECT "countries".* FROM "countries" WHERE "countries"."roast_id" = $1 [["roast_id", 76]]
Note Load (0.3ms) SELECT "notes".* FROM "notes" INNER JOIN "tastings" ON "notes"."id" = "tastings"."note_id" WHERE "tastings"."roast_id" = $1 [["roast_id", 76]]
(0.6ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 76], ["commentable_type", "Roast"]]
Rendered comments/_form.html.erb (3.9ms)
(0.7ms) SELECT COUNT(*) FROM "roasts" WHERE "roasts"."roaster_id" = 3
Roast Load (0.7ms) SELECT "roasts".* FROM "roasts" WHERE "roasts"."roaster_id" = 3
Rendered partials/_footer.html.erb (1.4ms)
Rendered roasts/show.html.erb within layouts/application (42.1ms)
Completed 200 OK in 274ms (Views: 260.5ms | ActiveRecord: 6.0ms)
模型
class Roast < ApplicationRecord
has_many :tastings
has_many :countries
has_many :notes, through: :tastings
has_many :comments, as: :commentable
accepts_nested_attributes_for :countries
has_many :regions
belongs_to :roaster
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
accepts_nested_attributes_for :regions
belongs_to :roast
class Region < ApplicationRecord
belongs_to :country, inverse_of: :regions
烤/ show.html.erb
<tbody>
<% @roast.countries.each do |countries| %>
<tr>
<th>
<%= roast.countries.country_name %>
</th>
<% @roast.countries.region.each do |region| %>
<th>
<%= region.region_name %>
</th>
</tr>
<% end %>
<% end %>
</tbody>
roast_controller.rb
def show
@roast = Roast.friendly.find(params[:id])
@commentable = @roast
@comments = @commentable.comments
@comment = Comment.new
@sameroaster = Roast.where(roaster: @roast.roaster)
@samecountry = Roast.where(country: @roast.country)
@roastcount = Roast.where(roaster: @roast.roaster)
@countries = @roast.countries
@regions = @roast.regions
end
模式
create_table "countries", force: :cascade do |t|
t.string "country_name"
t.integer "region_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "roast_id"
t.string "slug"
end
create_table "regions", force: :cascade do |t|
t.string "region_name"
t.integer "country_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "roasts", force: :cascade do |t|
t.string "roaster"
t.string "name"
t.string "bestfor"
t.string "beans"
t.string "roast"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tastingnotes"
t.string "slug"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "roaster_id"
t.integer "country_id"
t.boolean "decaf"
t.boolean "coldbrew"
t.integer "region_id"
t.index ["country"], name: "idx_roasts_country"
t.index ["country_id"], name: "idx_roasts_country_id"
t.index ["roaster_id"], name: "idx_roasts_roaster_id"
答案 0 :(得分:0)
根据问题
如果你想要
Roast有很多国家
和
国家有很多地区
进行以下更改
create_table "roasts", force: :cascade do |t|
t.integer "country_id" #remove country_id column from roasts
t.integer "region_id" #remove region_id column from roasts
t.index ["country_id"], name: "idx_roasts_country_id" #remove
end
create_table "countries", force: :cascade do |t|
t.integer "region_id" #remove region_id column from countries
end
你的模型结构应该是这样的
class Roast < ApplicationRecord
has_many :tastings
has_many :countries
has_many :notes, through: :tastings
has_many :comments, as: :commentable
accepts_nested_attributes_for :countries
#remove has_many regions for roast
# because as per your question country has many regions not roast
has_many :regions #remove this
belongs_to :roaster
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
accepts_nested_attributes_for :regions
belongs_to :roast
class Region < ApplicationRecord
belongs_to :country, inverse_of: :regions
end
和控制器操作和视图应该类似
def show
@roast = Roast.includes(countries: :regions)
.friendly
.find(params[:id])
@commentable = @roast
@comments = @commentable.comments
@comment = Comment.new
@sameroaster = Roast.where(roaster: @roast.roaster)
@roastcount = Roast.where(roaster: @roast.roaster).count
end
视图
<table>
<thead>
<tr>
<th>#</th>
<th>Country</th>
<th>Regions</th>
</tr>
</thead>
<tbody>
<% @roast.countries.each_with_index do |country, index| %>
<tr>
<td><%= index + 1 %></td>
<td><%= country.name %></td> // change this to country_name if your field is different in contries schema.
<td><%= country.regions.map(&:name).join(', ') %></td>// change this to region_name if your field is different in regions schema.
</tr>
<% end %>
</tbody>
</table>
我希望这会有所帮助。