Jngai1297
Jngai1297

Reputation: 2495

Getting Undefined Method Model Tracking Public_Activity Gem

I am going through the railscast on public activity

http://railscasts.com/episodes/406-public-activity?view=asciicast

In view/activities/index.html

I have this in my index.html

<% @activities.each do |activity| %>
  <div class="activity">
  <%= link_to activity.owner.company_name, activity.owner if activity.owner %>
  added a job <%= link_to activity.trackable.job.title, activity.trackable.job %>
  </div>
<% end %>

I have a job model and a company model

I have this in my job.rb model

include PublicActivity::Model
tracked owner: Proc.new{ |controller, model| controller.current_company }

and here is my activities index controller method

def index
    @activities = PublicActivity::Activity.order_by(:created_at.desc)
  end

I am getting

undefined method `job' for #<Job:0x007fda6e0384c8>

so this part is not working

<%= link_to activity.trackable.job.title, activity.trackable.job %>

but the funny thing is it works when I do

<%= activity.inspect %>

and

<%= link_to activity.owner.company_name, activity.owner if activity.owner %>

Here is one record of my activity

#<PublicActivity::Activity _id: 526981707242c02d96000006, created_at: 2013-10-24 20:22:08 UTC, updated_at: 2013-10-24 20:22:08 UTC, trackable_type: "Job", trackable_field: nil, trackable_id: "526981707242c02d96000005", owner_type: "Company", owner_field: nil, owner_id: "5268454701e6af3eb9000003", recipient_type: nil, recipient_field: nil, recipient_id: nil, key: "job.create", parameters: {}, _type: "PublicActivity::Activity">

You can see that the public activity is tracking the job model. The railscasts is dated in february and I can't find 'trackable' in the gem doc, has it been updated and you have to do it another way?

if it helps I am using devise for the company model

Updated, As Requested

When I do

 <%= activity.trackable %>

in my views/activities/index.html I am getting this

#<Job:0x007f882f47a248> #<Company:0x007f882f608a10> #<Company:0x007f882e0a4250> #<Job:0x007f882e0ac7c0> #<Job:0x007f882e0f6d98> #<Job:0x007f882e3d0eb0> #<Company:0x007f882e6319e8> #<Company:0x007f882e09ab60> #<Company:0x007f882e0ef0e8> #<Company:0x007f882e1c9658> #<Company:0x007f882e1979a0> #<Job:0x007f882e77b038> #<Company:0x007f882f612c90> #<Company:0x007f882ca7ffb0> #<Company:0x007f882f25dcd0> #<Job:0x007f882f267cf8> #<Company:0x007f882f2af148> #<Company:0x007f882c9d3648> #<Company:0x007f882c9d6f78> #<Job:0x007f882f5381a8> #<Job:0x007f882c999790> #<Job:0x007f882bd1e510> #<Job:0x007f882f121ad8> #<Job:0x007f882ca49208> #<Job:0x007f882c82c420> #<Job:0x007f882c921920> #<Job:0x007f882c923db0> #<Company:0x007f882c9ac318> #<Company:0x007f882c86f568> #<Job:0x007f882c872420> #<Job:0x007f882ca15a98> #<Job:0x007f882ca17d48> #<Job:0x007f882ca5eec8> #<Job:0x007f882ca698c8> #<Job:0x007f882c9c4328> #<Job:0x007f882c9c7460> #<Job:0x007f882c99f960> #<Job:0x007f882c9a2548> #<Job:0x007f882b379500> #<Company:0x007f882b37bd00> #<Company:0x007f882b386bd8> #<Job:0x007f882b388118> #<Company:0x007f882d057e88> #<Company:0x007f882e0af6c8> #<Company:0x007f882b0d9098> #<Company:0x007f882ca508f0> #<Company:0x007f882c9e36d8> #<Company:0x007f882ca6f6b0> #<Job:0x007f882b180848> #<Job:0x007f882b15e798> #<Company:0x007f882b1613f8> #<Company:0x007f882b1035a0> #<Company:0x007f882b185f28> #<Company:0x007f882f110530>

and when I do this instead <%= activity.owner %>

I am getting this output

#<Company:0x007f882c9322c0> #<Company:0x007f882c93d558> #<Company:0x007f882c940578>

Since I am tracking the changes in my company model, I have this as well

include PublicActivity::Model
tracked

I think everything is good but I just can't draw the job record from my activities out into the view?

Updated

I am using mongoid, here is my job.rb model

class Job


    include Mongoid::Document
    include Mongoid::Timestamps

    include PublicActivity::Model
        tracked owner: Proc.new{ |controller, model| controller.current_company }

    field :title 
    field :description
    field :skills
    field :education
    field :location

    belongs_to :company

end 

Upvotes: 0

Views: 1243

Answers (3)

Piotrek Okoński
Piotrek Okoński

Reputation: 597

I'm the creator of public_activity.

This:

activity.trackable.job.title

is definitely incorrect since you are calling a job method on a Job object, which doesn't make sense. trackable is your Job object.

Make sure there is title attribute for that job, because it looks like a problem with your Job model, not p_a's trackable relation.

Upvotes: 3

tmarkiewicz
tmarkiewicz

Reputation: 515

Not sure if you solved this, but I was having the exact same issue until I started inspecting trackable. Similar to d_ethier's answer, to fix this issue replace

<%= link_to activity.trackable.job.title, activity.trackable.job %>

with

<%= link_to activity.trackable.title, activity.trackable %>

To test you can also display the following

<%= activity.trackable.inspect %>

If inspecting trackable doesn't display your model's title, you have an issue outside of public_activity.

Upvotes: 1

d_ethier
d_ethier

Reputation: 3911

Not that familiar with the gem, but it looks like trackable maps to the job object in the polymorphic join. Chaining job may not be necessary.

<%= link_to activity.trackable.title, activity.trackable %>

Upvotes: 1

Related Questions