Figuring Out How To Access Methods

14 views
Skip to first unread message

Scott Le gendre

unread,
Jul 1, 2016, 9:52:27 AM7/1/16
to rubyonra...@googlegroups.com
I have an advertiser model and an experiment model. I've setup the
associations as follows:

Advertiser has_many :experiments
Experiment belongs_to :advertisers

The experiments table has a column titled "experiment_type", which can
either be AOV or Conversion. I am trying to display experiments for the
particular advertiser by experiment_type.

I can successfully display ALL of the experiments by advertiser with the
following iteration

<% @advertiser.experiments.each do |experiments| %>
<td><%= experiments.id %></td>
<td><%= experiments.name %></td>
<% end %>

Or I can successfully display all the experiment_type with the following
iteration

<% @aov.each do |experiments| %>
<td><%= experiments.id %></td>
<td><%= experiments.name %></td>
<% end %>

What I cannot figure out is how to show the experiment_type by
advertiser. I thought something like

<% @advertiser.aov.each do |experiments| %> would work, but it gives me
an undefined method `aov' for #<Advertiser:

Any help would be appreciated. Thanks in advance.

--
Posted via http://www.ruby-forum.com/.

Hassan Schroeder

unread,
Jul 1, 2016, 10:22:50 AM7/1/16
to rubyonrails-talk
On Fri, Jul 1, 2016 at 6:52 AM, Scott Le gendre <li...@ruby-forum.com> wrote:
> I have an advertiser model and an experiment model. I've setup the
> associations as follows:
>
> Advertiser has_many :experiments
> Experiment belongs_to :advertisers
>
> The experiments table has a column titled "experiment_type", which can
> either be AOV or Conversion. I am trying to display experiments for the
> particular advertiser by experiment_type.

It sounds like you wanted to do this:

http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Colin Law

unread,
Jul 1, 2016, 11:17:03 AM7/1/16
to Ruby on Rails: Talk
On 1 July 2016 at 14:52, Scott Le gendre <li...@ruby-forum.com> wrote:
> I have an advertiser model and an experiment model. I've setup the
> associations as follows:
>
> Advertiser has_many :experiments
> Experiment belongs_to :advertisers
>
> The experiments table has a column titled "experiment_type", which can
> either be AOV or Conversion. I am trying to display experiments for the
> particular advertiser by experiment_type.
>
> I can successfully display ALL of the experiments by advertiser with the
> following iteration
>
> <% @advertiser.experiments.each do |experiments| %>
> <td><%= experiments.id %></td>
> <td><%= experiments.name %></td>
> <% end %>
>
> Or I can successfully display all the experiment_type with the following
> iteration
>
> <% @aov.each do |experiments| %>
> <td><%= experiments.id %></td>
> <td><%= experiments.name %></td>
> <% end %>
>
> What I cannot figure out is how to show the experiment_type by
> advertiser.

Something like
@advertiser.experiments.where(experiment_type: "AOV").each do |experiment|
will get you all the AOV experiments (I have assumed that this is
actually a string "AOV").
Note that I have said do |experiment| with singular experiment. This
makes no difference to the way the code works but makes it clear that
this is a single experiment so that experiment.name looks meaningful.

In fact you would be better to write scopes for AOV and Conversion as
then the code will read even better and you would save some typing.

Colin
Reply all
Reply to author
Forward
0 new messages