【发布时间】:2019-03-20 21:21:01
【问题描述】:
我正在尝试提取与列表关联的姓名、联系人类型和公司,但我在 ActiveRecord 中进行渲染时遇到了困难。表(Contacts、Company、ContactType、连接表ListingContacts和Listing)如下:
Contacts.rb
belongs_to :company #trying to access `name` attribute in view
belongs_to :contact_type #trying to access `label` attribute in view
Company.rb
belongs_to :account
has_many :contacts
使用连接表通过Listing.rb 访问,Listing_contacts.rb:
has_many :listing_contacts
has_many :contacts, through: :listing_contacts
通过listings_controller访问:
def show
@listing = Listing.includes(*LISTING_EAGER_LOADED_ASSOCIATIONS).find(params[:id])
end
private
def listing_params
params.require(:listing).permit(:name, :address, :description, images_attributes: [:file, :unprocessed_image_url], listing_contacts_attributes: [:contact_id])
end
使用LISTING_EAGER_LOADED_ASSOCIATIONS 如下(这是此应用程序中的一种常见格式,因为它变得非常广泛 - 我一直在避免弄乱它,它会访问问题中的模型不涉及的其他模型:
LISTING_EAGER_LOADED_ASSOCIATIONS = [ rfid_tags: : 可追踪, selected_selections: :rfid_tag, staging_selections: :rfid_tag, staged_selections: :rfid_tag, destage_selections: :rfid_tag, unstaged_selections: :rfid_tag, ]
如前所述,我正在努力在视图中传达这些关联 - 这是我正在处理的内容,基于页面其余部分的模板 - 访问 listing_contacts 正在工作通过控制台,但在那之后我停止了:
<%= @listing.listing_contacts.each do |contact| -%>
<div class="row">
<div class="col-sm-6">
<h5 class="subtle-header">Name:</h5>
<h3 class="inline"><%= show(contact.name) %></h3> #working!
</div>
</div>
<div class="col-sm-6">
<h5 class="subtle-header">Address</h5>
<h3 class="inline"><%= show(contact.address) %></h3> #working!
</div>
</div>
<div class="row">
<div class="col-sm-6">
<h5 class="subtle-header">Contact Type:</h5>
<h3 class="inline"><%= show(#reaches through contacts to contact_type.label ) %></h3> #not working
</div>
<div class="col-sm-6">
<h5 class="subtle-header">Company:</h5>
<h3 class="inline"><%= show(#reaches through contacts to company.name) %></h3> #not working
</div>
</div>
<% end %>
structure.sql 供参考 - 使用 SQL 视图而不是模式;
CREATE TABLE public.listing_contacts (
id integer NOT NULL,
listing_id integer,
contact_id integer,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
CREATE TABLE public.contacts (
id integer NOT NULL,
company_id integer,
contact_type_id integer,
name character varying,
phone character varying,
email character varying,
address text,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
CREATE TABLE public.companies (
id integer NOT NULL,
account_id integer,
name character varying,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
CREATE TABLE public.contact_types (
id integer NOT NULL,
label character varying,
show_name boolean DEFAULT false,
show_phone boolean DEFAULT false,
show_address boolean DEFAULT false,
show_email boolean DEFAULT false,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
【问题讨论】:
标签: ruby-on-rails-4 model-view-controller activerecord view associations