0

I have an issue and I'm trying to figure out the best/simplest way to resolve this...

I have multiple users on my app. the app allows users to add books to a read-queue list (like a list of books a user can keep tabs on).

Originally, I set the read-queue list as a global array. However this meant that multiple users were adding books to a single global array. If user1 added 3 books, user2 would also see those 3 books. Or if user1 cleared the read-queue list, user2's books were cleared too.

Possible Solutions:

  1. Maybe I can create a key in the array to associate a portion of it to a user, but I imagine that the array would get pretty large...

  2. Maybe create a db table "queue-list".. but that seems like a lot to do for something like this..

  3. Maybe create a global array unique for each user. (I like this one, but not sure how to do that)

Hope this is clear, I'm pretty new to rails so any help is appreciated!

my attempt at solution 3:

books_controller.rb

def add_book_to_queue
  user = User.find(params[:id])
  user.read_queuelist.push(params[:book_info])
  @readlist = user.read_queuelist
end

user.rb

def read_queuelist
  $read_queuelist ||= Array.new
end

view

<%= @readlist %> 

3 Answers 3

0

If your app is simple enough, you could make an array for each user as it's shown below. But if your application is meant to handle a lot of users and tons of books, you should look towards DB integration.

class Book
  attrr_accessor :title, :author

  def initialize(t, a)
    @title, @author = t, a
  end
end

class User
  attr_accessor :books

  def initialize
    @books =  Array.new
  end

  def add(b)
    @books << b
  end
end

joe = User.new

joe.add(Book.new("moo", "foo"))

puts "Joe likes #{joe.books.length} books"

UPD: for rails, take a look at has_many and belongs_to associations.

Sign up to request clarification or add additional context in comments.

4 Comments

hmm thanks for your response~ I think this is the best solution, ill try it out
Note: this is the best solution if your application is simple. If you are aiming for a business one you must be sure it would not fail. In that case you should use database.
a few hundreds, maybe few thousands or even millions. depends on how complicated your classes are =)
I tried using this solution for a global array, but it still adds books to a single array. I've updated my question to post my code..
0

You can create lots of global hashes with Redis. It has the advantage of scaling and being optionally persistant (via backups). See http://redis.io/commands#hash and http://redistogo.com/ for free Redis hosting.

Comments

0

Since you're new to Rails, I think you'll find that using ActiveRecord to persist your model data to a database ends up being the simplest solution.

Comments

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.