Hello, I have a model, Feed, that has and belongs to many FilteredUsers. In this case I have implemented it through a has_many :through relationship.
class Feed < ActiveRecord::Base
has_many :denials, :dependent => :destroy
has_many :filtered_users, :through => :denials
I would like to create a record if it doesn't exist or find the object if it does exist. When I try and use the find_or_initialize_by (or find_or_create_by) an exception is thrown saying undefined method 'feed_id=' for <FilteredUser..
Here is the code:
feed = Feed.find(params[:id])
user = feed.filtered_users.find_or_initialize_by_user_url(params[:user_url])
if params[:status] == "block"
feed.filtered_users << user
else
feed.filtered_users.delete(user)
end
feed.save
Any suggestions on how to fix this or how to work around it DRYly?
Thanks!