Rails: 如何通过两个不同的关系连接两个模型?
Rails: how to join two models through two different relations?
我有两个模型:Saft(一本杂志)和 Keyword。每个 "Saft" 由一系列关键字定义,但也有一个标题,标题始终是其关键字之一。 Saft 和关键字模型通过 HABTM 连接 table 连接,以便提取所有关键字,现在我正在尝试将关键字 table 中的标题提取到 saft/show。html.erb,也是。我正在尝试使用 class_name 选项来提取标题。因此我创建了版本模型。
class Saft < ActiveRecord::Base
# attr_accessible :colour, :cover_alt, :description, :number, :short
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
has_one :title, :through => :edition, :class_name => "keyword"
has_one :edition
end
class Keyword < ActiveRecord::Base
# attr_accessible :word, :description
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
belongs_to :issue, :through => :edition, :class_name => "saft"
belongs_to :edition
end
class Edition < ActiveRecord::Base
# attr_accessible :saft_id, :keyword_id
belongs_to :title
belongs_to :issue
end
class SaftsController < ApplicationController
def show
@saft = Saft.find(params[:id])
end
show.html.erb
<%= @saft.title.upcase %>
我收到以下错误:
Started GET "/safts/2" for 127.0.0.1 at Sat Feb 10 17:31:28 +0100 2018
Connecting to database specified by database.yml
Processing by SaftsController#show as HTML
Parameters: {"id"=>"2"}
Saft Load (1.8ms) SELECT `safts`.* FROM `safts` WHERE `safts`.`id` = ? LIMIT 1 [["id", "2"]]
Image Load (0.3ms) SELECT `images`.* FROM `images` WHERE `images`.`saft_id` = 2
Rendered safts/show.html.erb within layouts/public (35.0ms)
Completed 500 Internal Server Error in 103ms
ActionView::Template::Error (uninitialized constant Saft::keyword):
29: </div>
30: <div class="saft_box col-content">
31: <div class="saft_keyword">
32: <strong><%= @saft.title.upcase %></strong>
33: </div>
34: <div class="saft_description">
35: <p><%= @saft.description %></p>
app/views/safts/show.html.erb:32:in `_app_views_safts_show_html_erb___758994895_2167416580'
我怎样才能让它工作?
当取消注释@saft.title.upcase 时,此实现也破坏了 Saft 关键字关联,我通过取消注释关键字模型中的 belongs_to :issue 部分再次工作。
您的模型有一些变化:
class Saft < ActiveRecord::Base
#You don't need attr_accessible for fields in safts table
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
#The specific keyword that acts as title.
#You need a new field in safts table named title_id which references to a Keyword.
belongs_to :title, class_name => "Keyword", :foreign_key => 'title_id'
end
class Keyword < ActiveRecord::Base
#You don't need attr_accessible for fields in keywords table
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
end
要获得 Saft 称号,请使用 @saft.title.word.upcase if @saft.title
我认为您的模型不需要任何其他东西来满足您 OP 中的用例。
我有两个模型:Saft(一本杂志)和 Keyword。每个 "Saft" 由一系列关键字定义,但也有一个标题,标题始终是其关键字之一。 Saft 和关键字模型通过 HABTM 连接 table 连接,以便提取所有关键字,现在我正在尝试将关键字 table 中的标题提取到 saft/show。html.erb,也是。我正在尝试使用 class_name 选项来提取标题。因此我创建了版本模型。
class Saft < ActiveRecord::Base
# attr_accessible :colour, :cover_alt, :description, :number, :short
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
has_one :title, :through => :edition, :class_name => "keyword"
has_one :edition
end
class Keyword < ActiveRecord::Base
# attr_accessible :word, :description
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
belongs_to :issue, :through => :edition, :class_name => "saft"
belongs_to :edition
end
class Edition < ActiveRecord::Base
# attr_accessible :saft_id, :keyword_id
belongs_to :title
belongs_to :issue
end
class SaftsController < ApplicationController
def show
@saft = Saft.find(params[:id])
end
show.html.erb
<%= @saft.title.upcase %>
我收到以下错误:
Started GET "/safts/2" for 127.0.0.1 at Sat Feb 10 17:31:28 +0100 2018
Connecting to database specified by database.yml
Processing by SaftsController#show as HTML
Parameters: {"id"=>"2"}
Saft Load (1.8ms) SELECT `safts`.* FROM `safts` WHERE `safts`.`id` = ? LIMIT 1 [["id", "2"]]
Image Load (0.3ms) SELECT `images`.* FROM `images` WHERE `images`.`saft_id` = 2
Rendered safts/show.html.erb within layouts/public (35.0ms)
Completed 500 Internal Server Error in 103ms
ActionView::Template::Error (uninitialized constant Saft::keyword):
29: </div>
30: <div class="saft_box col-content">
31: <div class="saft_keyword">
32: <strong><%= @saft.title.upcase %></strong>
33: </div>
34: <div class="saft_description">
35: <p><%= @saft.description %></p>
app/views/safts/show.html.erb:32:in `_app_views_safts_show_html_erb___758994895_2167416580'
我怎样才能让它工作?
当取消注释@saft.title.upcase 时,此实现也破坏了 Saft 关键字关联,我通过取消注释关键字模型中的 belongs_to :issue 部分再次工作。
您的模型有一些变化:
class Saft < ActiveRecord::Base
#You don't need attr_accessible for fields in safts table
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
#The specific keyword that acts as title.
#You need a new field in safts table named title_id which references to a Keyword.
belongs_to :title, class_name => "Keyword", :foreign_key => 'title_id'
end
class Keyword < ActiveRecord::Base
#You don't need attr_accessible for fields in keywords table
has_and_belongs_to_many :safts, :join_table => "safts_keywords"
end
要获得 Saft 称号,请使用 @saft.title.word.upcase if @saft.title
我认为您的模型不需要任何其他东西来满足您 OP 中的用例。