Я пытаюсь найти имена, типы контактов и компании, связанные с листингом, но я борюсь с рендерингом тогда в 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,
destaged_selections :: rfid_tag,
unstaged_selections:: rfid_tag,
]
Как упоминалось ранее, я изо всех сил пытаюсь передать эти ассоциации в представлении - вот то, с чем я работаю, основываясь на шаблоне с остальной части страницы - доступ к list_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
);