Property has many lists. List has many properties. Users can add properties to a list , here user must be owner of those properties which he want to add.
Here is my code:
class List < ActiveRecord::Base
has_many :propertyships
has_many :properties, :through => :propertyships
end
class Propertyship < ActiveRecord::Base
belongs_to :list
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :propertyships
has_many :lists, :through => :propertyships
end
in view:
<%= simple_form_for(@list, url: add_property_list_path, :method => :put) do |f| %>
<%= f.collection_check_boxes(:property_ids, @user_all_properties, :id, :street_address) %>
<%= f.submit "Add Property" %>
<% end %>
in routes:
resources :lists do
member do
put 'add_property'
end
end
in controller:
def add_property
@list = List.find(params[:id])
@list.update_attributes(list_params)
end
private
def list_params
params.require(:list).permit(:name, :user_id, property_ids: [])
end
when I checked properties and I click "Add property" button as other user , its add checked properties but remove old properties that I have in that particular list. what a I doing wrong??