I have a database that contains users and groups with a has_and_belongs_to_many relationship. When a new group is added, it gets created but the user's membership to the group doesn't seem to propagate until I clear the cache or login with an incognito window. I know it's getting saved correctly, it just doesn't seem to be loading until the cache is cleared. This only recently started happening and I can't figure out why! Any help would be greatly appreciated.
From the models:
class User < ActiveRecord::Base
has_many :services
has_many :recipes
has_and_belongs_to_many :groups
attr_accessible :recipes, :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :recipes
attr_accessible :description, :title, :recipe, :picture, :featured, :user_id
end
The create group method:
def create
@user = User.find(current_user.id)
@group = Group.new(params[:group])
@group.user_id = @user.id
@user.groups << @group
redirect_to group_path(@group)
end
Displaying the group memberships of a user -- this won't update until the cache is cleared:
<% @user.groups.each do |group| %>
<% if group %>
<p class="group-title"><a href="<%= group_path(group) %>"><%= group.title %></p>
<% @latestpic = Recipe.where("group_id = ?", group).limit(1).order("created_at DESC") %>
<% if @latestpic.exists? %>
<% @latestpic.each do |pic| %>
<%= image_tag(pic.picture.url(:medium)) %>
<% end %></a>
<% else %>
<%= image_tag "http://placehold.it/300x300" %>
<% end %>
<br></br>
<% end %>
<% end %>