Hi..Im trying to figure out this HABTM relationship issue to store in the interests of users within my database. The interests table has a list of different interests with id and name (i.e: id=1 , name = 'Music')
I have a users model => user.rb
has_and_belongs_to_many :interests
and an interests model => interest.rb
has_and_belongs_to_many :users
Now I'm trying to edit or update the users choice of interests from a list of checkboxes. The controller looks like so =>
def edit
#@interests = Interest.find(:all)
@title = "Edit Interest"
@user = User.find(session[:user_id])
@user.interest ||= Interest.new
interest = @user.interest
if param_posted?(:interest)
if @user.interest.update_attributes(params[:interest])
flash[:notice] = "Changes saved."
redirect_to :controller => "users", :action => "index"
end
end
end
and the param_posted function looks like this
def param_posted?(sym)
request.post? and params[sym]
end
The view logic looks like this:
<% for interest in @interest %>
<div>
<%= check_box_tag "user[interest_id][]", interest.id, @user.interests.include (interest) %>
<%= interest.name %>
</div>
<% end %>
I thought everything looked kosher but when I run the view I get the error :
NoMethodError in InterestController#edit - undefined method `interest' for # User:0x4797ddc
Do i need to create a separate model/table that connects the interest of users to events? like InterestsofUsers ( id, user_id, interest_id)? I thought the HABTM relationship would eliminate the need for that...
Confused