2

I'm trying to create some kind of activity feed, and I'm having trouble getting the values I need to.

I have a controller with this action

  def show
    @user = User.find(params[:id])
    @ideas = Idea.find(@user)
    @lists = List.find(@user)
    @li_array = (@lists + @ideas).sort{|a,b| -(a.created_at <=> b.created_at)}      
  end

I'm new to rails, and I'm trying to put together an array of values from the users of both ideas and lists. But I figured out that I can't do what I'm doing now because it will only pass the current user's id to match an id for Idea/List, but what I need to do is find the user's id column and search through based on @user.

I need to get all values from the users, what is the best method?

models

user.rb

  has_many :lists
  has_many :ideas, :through => :lists

list.rb

  has_many :ideas
  belongs_to :user

idea.rb

  belongs_to :list
  belongs_to :user

Thanks

1 Answer 1

2

You can simply do:

@user = User.find(params[:id])
@ideas = @user.ideas
@lists = @user.lists
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. This is very basic stuff, so you may want to have a look at Rails Guides to learn more about how associations work in Rails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.