ruby-on-railsangularjspublic-activity

How do display activity owner name in Angular?


I'm trying to build a activity stream in my rails/angular application using the Public Activity gem and the Railscast. It's a pretty easy set up on the rails side, but I'm having some trouble with the angular side of things.

I've tracked my movie model. So when a movie is added it's recorded as a new activity. I've created a template, service and controller (on the angular side) to display the activity.

The template,

%ul{"ng-repeat" => "activitie in activities"}
  %li {{ activitie }}

When I view the template in my app it displays like this (which is expected).

{"id":2,"trackable_id":5,"trackable_type":"Movie","owner_id":1,"owner_type":"User","key":"movie.create","parameters":{},"recipient_id":null,"recipient_type":null,"created_at":"2015-12-30T11:55:06.766Z","updated_at":"2015-12-30T11:55:06.766Z"}

Ofcourse this is not how I want to display the data, but here it gets tricky. In the railscast Ryan adds this code to his template,

<% @activities.each do |activity| %>
  <div class="activity">
    <%= link_to activity.owner.name, activity.owner if activity.owner %>
    <%= render_activity activity %>
  </div>
<% end %>

So he uses activity.owner.name to present the name. But I have no idea how to get this result in Angular. The owner_id is the id of the user that added the movie, but I don't know how to display it through Angular.


Solution

  • You can join with the users table to incorporate the users name.

    PublicActivity::Activity.
      joins("INNER JOIN users ON users.id = activities.owner_id").
      select("activities.*, users.name")
    

    Now you should be able to call activity.name.