开发者

Opinion on whether to use a hash or an array, pushing new element in Rails

I'm working on a Rails 3开发者_JAVA百科 app where a user's profile has_many :todos. The user has three categories of :todos: two that are added to by clicking buttons on other pages, and one that they can add to themselves (through a status-update-like form).

So I'm interested in your thoughts on:

  1. How to best set it up, particularly whether to use arrays or hashes. I'd like to count all of the :todos as well as list the string values for the individual categories.
  2. The best way to push to the two categories that aren't like status updates. I'd attach the push to a button most likely, so the push would come on click or something.

UPDATE: Just to check...

The Profile Model:

has_many :todo, :through => :profiles_todos
has_many :profiles_todos

The ProfilesTodos Model:

belongs_to :profile
belongs_to :todo

And the profiles_todos Migration:

(in self.up)
  t.string :category 
  t.integer :profile_id 
  t.integer :todo_id


I would recommend creating a join model UsersTo-dos that has a field category:string. This way the to-dos can be unique in the database, and shared between multiple users. Use has_many :users_to-dos along with has_many :to-dos, :through => :users_to-dos to make @user.to-dos an array of to-dos, and @user.users_to-dos an array of join objects with the category and the to-do object. You can then use the push method to add to the to-dos, or your can build new users_to-dos objects and add the category and to-do reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜