Kevin Pruett
2012-Mar-03 20:22 UTC
Properly Setting up Rails Associations for "Favoriting" Items
I''m writing my first Rails app and I''m stuck at the database level. I have 2 models: User and Mission. They are connected very *unremarkably* like so: User has_many :missions, dependent: :destroy Mission belongs_to :user I would like other users to be able to interact with missions by either: *liking them* and/or *marking them as completed* This is all very vanilla stuff here, but I''m struggling with adding this additional layer of interaction into the models. I started by creating a table Connection with attributes user_id mission_id liked:booleancompleted:boolean I added the following: User has_many :connections, dependent: :destroy Connection belongs_to :user Connection belongs_to :mission Do I need a has_many :through relationship? I''m trying to preserve the ownership between a User and a Mission ( belongs_to association ). I''m reading about polymorphic associations and am wondering if they are applicable? Any help would be greatly appreciated. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/rj0_oOOrrV0J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2012-Mar-04 12:35 UTC
Re: Properly Setting up Rails Associations for "Favoriting" Items
On 3 March 2012 20:22, Kevin Pruett <pruett.kevin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m writing my first Rails app and I''m stuck at the database level. I have 2 > models: User and Mission. They are connected very unremarkably like so: > > User has_many :missions, dependent: :destroy > Mission belongs_to :user > > I would like other users to be able to interact with missions by > either: liking them and/or marking them as completed > > This is all very vanilla stuff here, but I''m struggling with adding this > additional layer of interaction into the models. > > I started by creating a table Connection with > attributes user_id mission_id liked:booleancompleted:booleanSince each mission belongs to just one user you can achieve what you want just by adding the liked and completed booleans to the missions table. You do not need another table. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.