在视图中呈现关联的ActiveRecord属性

时间:2019-03-20 21:21:02

标签: ruby-on-rails-4 model-view-controller activerecord view associations

我正在尝试提取与列表相关的姓名,联系方式和公司,但是我却在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::trackable,       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
);

2 个答案:

答案 0 :(得分:0)

您尝试使用button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String webastring = web.getText().toString(); Toast.makeText(getApplicationContext(),webastring,Toast.LENGTH_SHORT).show(); }}); 而不是<%= lc.contacts.name %>吗?

答案 1 :(得分:0)

您可以执行<%= @ listing.contacts.each做|| contact |。 -%>

然后在循环中,您应该可以<%= show(contact.name) %>