ruby on rails - Model association in a to-do list with shareable list items -
even after reading http://guides.rubyonrails.org/association_basics.html , number of similar questions here on so, i'm still having problem visualising associations need include.
here's have far:
user:
has_one :list
list:
belongs_to :user has_many :list_items
list_item:
belongs_to :list
the problem i'm gonna need have functionality allow user @ else's list , add together of list_items his/her own list. meaning list_item can belong_to_many :lists
.
will need model facilitate has_many :through
relationship, or over-thinking it?
yes, on thinking. don't need has_many :through
. if user
has 1 list
. can add together list_item
list
. why need has_many :through
.
to add together list_item
user:
def add_list_item_to_user(user, list_item) user.list.list_items << list_item end
that's it.
if want access list_items
using user
can below:
user model:
has_many :list_items, :through => :list
form can access list_items
on user
user.list_items user.list_items << list_item
you have has_one
relation think don't need worry much through
. if want utilize can do.
ruby-on-rails model associations
No comments:
Post a Comment