在视图中呈现关联的 ActiveRecord 属性
Rendering associated ActiveRecord attributes in the view
我试图调出与列表关联的姓名、联系人类型和公司,但我在 ActiveRecord 中难以呈现。 tables (Contacts, Company, ContactType, join table ListingContacts and 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
使用连接的 table、Listing_contacts.rb
:
通过 Listing.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
with LISTING_EAGER_LOADED_ASSOCIATIONS
如下(这是这个应用程序中的一种常见格式,因为它变得非常广泛 - 我一直避免弄乱它,并且它访问问题中的其他模型不触摸:
LISTING_EAGER_LOADED_ASSOCIATIONS = [
rfid_tags::可追踪,
selected_selections: :rfid_tag,
staging_selections: :rfid_tag,
staged_selections: :rfid_tag,
destaged_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
);
你试过用<%= lc.contacts.name %>
代替<%= show(lc.contacts.name) %>
吗?
你可以做<%= @listing.contacts.each做|联系| -%>
然后在你的循环中,你应该能够做到 <%= show(contact.name) %>
我试图调出与列表关联的姓名、联系人类型和公司,但我在 ActiveRecord 中难以呈现。 tables (Contacts, Company, ContactType, join table ListingContacts and 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
使用连接的 table、Listing_contacts.rb
:
Listing.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
with LISTING_EAGER_LOADED_ASSOCIATIONS
如下(这是这个应用程序中的一种常见格式,因为它变得非常广泛 - 我一直避免弄乱它,并且它访问问题中的其他模型不触摸:
LISTING_EAGER_LOADED_ASSOCIATIONS = [ rfid_tags::可追踪, selected_selections: :rfid_tag, staging_selections: :rfid_tag, staged_selections: :rfid_tag, destaged_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
);
你试过用<%= lc.contacts.name %>
代替<%= show(lc.contacts.name) %>
吗?
你可以做<%= @listing.contacts.each做|联系| -%>
然后在你的循环中,你应该能够做到 <%= show(contact.name) %>