rails has_many through for multiple models - no method for nil:class -


i have model properties, residents , people(people outside city have not residents) , have owner join table set if property belongs resident, or person

here models

class property < activerecord::base     has_many :owners, dependent: :destroy     has_many :residents, through: :owners     has_many :people, through: :owners     belongs_to :ptype end  class resident < activerecord::base     has_many :owners, dependent: :destroy      has_many :properties, through: :owners end  class person < activerecord::base     has_many :owners, dependent: :destroy      has_many :properties, through: :owners end  class owner < activerecord::base     belongs_to :resident     belongs_to :property     belongs_to :person end 

i want have view show relation if owner.resident show properties or if owner.person show properties

 <% if @owner.resident %>   <ol><% @residents.properties.each |property| %>             <li><%= property.ptype.name %> , <%= property.address %></li>           <% end %>  </ol> <% end %>  <% if @owner.person %> <ol><% @people.properties.each |property| %>             <li><%= property.ptype.name %> , <%= property.address %></li>             <% end %>  </ol><% end %> 

when want show relation owner resident, got error no method properties nil:class @people , when want show relation owner person got oposite, error no method properties nil:class @residents

i understand because returns no record, thats why put there if

what doing wrong?

edit: use default controllers generated rails

class peoplecontroller < applicationcontroller   before_action :set_person, only: [:show, :edit, :update, :destroy]    def index     @people = person.all   end    def show   end    def new     @person = person.new   end    def edit   end    def create     @person = person.new(person_params)     respond_to |format|       if @person.save         format.html { redirect_to @person, notice: 'person created.' }         format.json { render :show, status: :created, location: @person }       else         format.html { render :new }         format.json { render json: @person.errors, status: :unprocessable_entity }       end     end   end    def update     respond_to |format|       if @person.update(person_params)         format.html { redirect_to @person, notice: 'person updated.' }         format.json { render :show, status: :ok, location: @person }       else         format.html { render :edit }         format.json { render json: @person.errors, status: :unprocessable_entity }       end     end   end    def destroy     @person.destroy     respond_to |format|       format.html { redirect_to people_url, notice: 'person destroyed.' }       format.json { head :no_content }     end   end   private      def set_person       @person = person.find(params[:id])     end      def person_params       params.require(:person).permit(:name, :email, :tel)     end end  class residentscontroller < applicationcontroller   before_action :set_resident, only: [:show, :edit, :update, :destroy]   def index     @residents = resident.all   end    def show     @residents = resident.find(params[:id])   end    def new     @resident = resident.new   end    def edit   end    def create     @resident = resident.new(resident_params)     respond_to |format|       if @resident.save         format.html { redirect_to @resident, notice: 'záznam bol úspešne vytvorený.' }         format.json { render :show, status: :created, location: @resident }       else         format.html { render :new }         format.json { render json: @resident.errors, status: :unprocessable_entity }       end     end   end    def update     respond_to |format|       if @resident.update(resident_params)         format.html { redirect_to @resident, notice: 'záznam bol úspešne upravený.' }         format.json { render :show, status: :ok, location: @resident }       else         format.html { render :edit }         format.json { render json: @resident.errors, status: :unprocessable_entity }       end     end   end    def destroy     @resident.destroy     respond_to |format|       format.html { redirect_to residents_url, notice: 'záznam bol úspešne zmazaný.' }       format.json { head :no_content }     end   end   private      def set_resident       @resident = resident.find(params[:id])     end     def resident_params       params.require(:resident).permit(:name, :birthdate, :birthid, :address, :email, :tel)     end end 

ok did wrong begining solution

<% if @owner.resident.present? %>   <ol><% @owner.resident.properties.each |property| %>             <li><%= property.ptype.name %> , <%= property.address %></li>           <% end %>  </ol> <% end %> <% if @owner.person.present? %> <ol><% @owner.person.properties.each |property| %>             <li><%= property.ptype.name %> , <%= property.address %></li>           <% end %>  </ol> <% end %> 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -