zhs
zhs

Reputation: 69

How to iterate through array in controller

I am making a Twitter clone using rails 4 just for practice. When a user is logged in, on the timeline I only want to display tweets of the people they follow (friends) and their own tweets in DESC order. I'm using tweets#index as my timeline. Currently I am displaying all tweets in the database to the user:

def index
  @tweets = Tweet.all.order("created_at DESC")
end

I added an instance variable called @user_friendships that contains the current logged in users friends, witch I can then iterate through and access their tweets. Here is what it now looks like:

def index
  @user_friendships = current_user.user_friendships.all
  @tweets = Tweet.all.order("created_at DESC")
end

I don't want to loop through user_friendships and grab it's friend and then tweets in the view as well as loop through the current_users tweets.

In the controller, I want to have one instance variable that contains the tweets of both the current_user and each friends tweets in user_friendships...so in the View I only have to iterate through the new array.

Model Code

### User.rb

has_many :tweets

has_many :user_friendships
has_many :friends, through: :user_friendships

acts_as_voter

def to_param
  username
end

### user_friendship.rb

belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'


### tweet.rb

belongs_to :user

Upvotes: 0

Views: 1157

Answers (3)

Nermin
Nermin

Reputation: 6100

def index
   @tweets = current_user.related_tweets.sort_by{|t| t.created_at}.reverse!
end

in model User

class User < ActiveRecord::Base
   # ...
   def related_tweets
      friends_tweets = friends.map{|f| f.tweets}
      return (tweets.to_a + friends_tweets).flatten
   end
   # ...
end

or other solution if you dont want to work with arrays

def index
   @tweets = Tweet.for_user(current_user).order(cretead_at: :desc)
end

in model Tweet

class Tweet < ActiveRecord::Base
   # ...
   def self.for_user user
      me_and_friends = [user.id] + user.friends.map(&:id)      
      where(:user_id => me_and_friends)
   end
   # ...
end

Upvotes: 1

Jose
Jose

Reputation: 1

You can do something like this:

def index
  @tweet_wall = Tweet.all.where(user_id: current_user.id).where(relationship between user and friend)
end

Second condition would depend on your model.

Upvotes: 0

Rustam Gasanov
Rustam Gasanov

Reputation: 15791

You didn't show the relations you have between tweets and users, but I assume you've got user has many tweets. Then, you can do something like this:

@tweets = Tweet.where(user_id: [current_user.id].concat(current_user.user_friendships.all.map(&:id)))

Upvotes: 1

Related Questions