Ruby on Rails -- Using multiple has_many associations to the same model -
i have simple program event can created user, , event class knows created it:
class event < activerecord::base # owner of event belongs_to :user end class user < activerecord::base # user has 0 n created events has_many :events end so far good, correct association behaviour when call "event.user" example, , creator of event can perform crud operations on his/her event.
however want ability user save events (not in database sense, can view in dashboard or such), , want use these 2 models. tried adding "user_saved_id" event model new column keep track of users save following:
class event < activerecord::base # owner of event belongs_to :user end class user < activerecord::base # user has 0 n created events has_many :events # user has 0 n saved events, including created event has_many :saved_events, foreign_key: 'user_saved_id', class_name: "event" end however having no such luck. when try initialize event creator have automatically saved his/her own event when he/she creates example, " undefined method `each' " in create method below (i using devise gem, , "current_user" helper function returns id of current signed in user):
# post /events # post /events.json def create @event = event.new(event_params) # associate event creator, works correctly @event.user = current_user # initialize user have saved own events, not working @event.user.saved_events = current_user ... i appreciate pointers can on getting wrong, if got association relationship wrong, , if overthinking , there simpler way go it. in advance.
the problem adding user event user_saved_id, 1 user ever able save event. if understand correctly, user able save events (that may or may not have created?) shown in dashboard. in case going need has_and_belongs_to_many or has_many through type relationship, , table. perhaps using has_many through.
class savedevent < activerecord::base belongs_to :user belongs_to :event end class user < activerecord::base # user has 0 n created events has_many :events has_many :saved_events # user has 0 n saved events through favorites table has_many :later_events, through: :saved_events end when create savedevent, can use created_at timestamp show when user saved it, or can store other information/metadata saving it.
having reverse set event allow list of users have saved event via event.saved_users. (sorry naming things 1 of hard problems)
class event < activerecord::base # owner of event belongs_to :user has_many :saved_events has_many :saved_users, through: :saved_events end then creating new event be
def create #wrap in transaction event.transaction #build event through association @event = current_user.events.create!(event_params) #save current users saved events current_user.saved_events.create!(event: @event) end end the transaction , ! methods make roll if cannot create event , add saved events, , not wind event isn't in saved/later events table when created.
when user wants save event, in controller that, need current_user.saved_events.create(event: event) them, , save it.
then in dashboard can current_user.later_events , collection of events have saved.
Comments
Post a Comment