Rails API with Grape, polymorphic: true, touch: true is not working -
i developing rails 4.2.6 api app number of polymorphic associations, problem touch: true not show updated object.
class post < activerecord::base belongs_to :postable, polymorphic: true, touch: true end class event < activerecord::base has_many :posts, as: :postable end class agenda < activerecord::base belongs_to :event, touch: true has_many :sessions, through: :days, class_name: 'agendasession' end class agendaday < activerecord::base belongs_to :agenda, touch: true has_many :sessions, class_name: 'agendasession' delegate :event, to: :agenda end class agendasession < activerecord::base belongs_to :agenda_day, touch: true delegate :event, to: :agenda_day has_many :posts, as: :postable end
i trying retrieve event's attribute 'hashtag' through post.postable. sake of argument lets have event hashtag: '#rock_events' , session belongs same event.
@event_post = event.first.posts.first @session_post = agendasession.first.posts.first # through console (and rspec): @event_post.postable.hashtag >> '#rock_events' @session_post.postable.event.hashtag >> '#rock_events' # edit event's hashtag: event.first.update(hashtag: '#pop_event') >> true @event_post.postable.hashtag >> '#rock_events' @session_post.postable.event.hashtag >> '#rock_events'
my current dirty fix is:
class post < activerecord::base belongs_to :postable, polymorphic: true, touch: true belongs_to :event, foreign_key: :postable_id, touch: true belongs_to :session, class_name: 'agendasession', foreign_key: :postable_id, touch: true end # , call post's parent's hashtag: @event_post.postable.posts.find(@event_post.id).event.hashtag @session_post.postable.posts.find(@session_post.id).session.event.hashtag
i understand i'm reloading associations in order updated event's hashtag. knows of fix or better work around predicament?
thank you
Comments
Post a Comment