0

I've been developing an app kind of like a Real Estate platform. But i've encounter son difficulties on a specific functionality.

I have two types of users: Owners and Users. Owner creates properties and user obviously needs to visualize them and interact with them. Both users are developed using devise, owners can create successfully properties, but users can't see them.

So, when I log in as a User I want to see some properties that the owners have been created, no matter in which order there displayed. I came with this issue because I need to create a filter so Users can find specific properties, but no filter can't be done if the user can`t see properties.

So this is my code so far:

Create_properties Migration

class CreateProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :properties do |t|
      t.string :name
      t.text :description
      t.integer :price
      t.string :address
      t.float :latitude
      t.float :longitude



      t.references :owner
      t.timestamps
    end
    add_index :properties, [:id, :created_at]
  end
end

Properties_controller.rb

class PropertiesController < ApplicationController
  before_action :set_property, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_owner!

  # GET /properties
  # GET /properties.json
  def index
    @properties = Property.all
  end

  # GET /properties/1
  # GET /properties/1.json
  def show

  end

  # GET /properties/new
  def new
    @property = current_owner.properties.build
    @property.amenities.build
    @property.services.build
    @property.build_propertytype

  end

  # GET /properties/1/edit
  def edit
  end

  # POST /properties
  # POST /properties.json
  def create
    @property = current_owner.properties.build(property_params)

    respond_to do |format|
      if @property.save
        format.html { redirect_to @property, notice: 'Tu propiedad ha sido creada!' }
        format.json { render :show, status: :created, location: @property }
      else
        format.html { render :new }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /properties/1
  # PATCH/PUT /properties/1.json
  def update
    respond_to do |format|
      if @property.update(property_params)
        format.html { redirect_to @property, notice: 'La propiedad ha sido actualizada.' }
        format.json { render :show, status: :ok, location: @property }
      else
        format.html { render :edit }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /properties/1
  # DELETE /properties/1.json
  def destroy
    @property.destroy
    respond_to do |format|
      format.html { redirect_to properties_url, notice: 'La propiedad ha sido eliminada!' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_property
      @property = Property.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def property_params
      params.require(:property).permit(:id, :description, :price, :sharable, :address, { pictures: [] }, amenities_attributes: [:id, :bathroom, :room, :pool, :gym, :kitchen, :terrace, :balcony, :living_room, :garage, :parking_lot, :green_areas], propertytype_attributes: [ :apartment, :building, :office, :local_comercial, :house, :terrain ], services_attributes: [:wifi, :electricity, :gas, :guard, :air_conditioning, :water, :include_furniture])
    end
end

Property Model

class Property < ApplicationRecord
    belongs_to :owner
    has_many :amenities
    has_many :services
    has_one :propertytype

    accepts_nested_attributes_for :propertytype
    accepts_nested_attributes_for :amenities
    accepts_nested_attributes_for :services

    mount_uploaders :pictures, PropertypictureUploader
end

User Model

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable, :trackable

  validates :rfc, presence: true

end

So, how can I make that the user see some properties? I would appreciate the help!

  • 1
    The real question is which properties would you like the user to see ? All properties or some(filtered based on some condition). To show all properties I think you can use most of what is done for `PropertiesController#index`. To show some you might have to apply some conditions and then show those properties (for the view part i believe you can refer to your implementation of the properties index action. – Alok Swain Jan 30 '20 at 07:19
  • Thanks Alok for the reply. Well technically I expect to see mostly the recent ones, since the user will use the filter to find specific properties in case the recent ones doesn't fit there needs. Using `PropertiesController#index` will allow the user to see them besides user and properties are different controllers? Or do I have to set something in the model? –  Jan 30 '20 at 07:36
  • 1
    To get recent properties all you would need to do is `Property.where("created_at > ?", Date.today - 1.day)` should give you all properties created since yesterday or use some similar logic. You may declare a scope on the Property model and use it but isnt mandatory. So no you wouldnt need to change the model. and once you get the property object you can access all its related associations or properties. – Alok Swain Jan 30 '20 at 07:46

1 Answers1

0

below are sample based from your information above, to find properties that created by owners

def index
  @properties = Owner.find_by_name('John').properties
  # this will list properties that owned by John
  @properties = Property.joins(:owner).all
  # this will list properties that has owner
  @properties = Property.joins(:owner).where('properties.price <= ?',1000000)
  # this will list properties that has owner and price is below 1 million
end
widjajayd
  • 6,090
  • 4
  • 30
  • 41
  • thanks a lot! Makes sense for me, just one quick question. What you put goes for the User controller or for the properties controller? Thanks for your reply! ☺️ –  Jan 30 '20 at 16:30
  • Never mind, I was able to made it fix with your approach. Thanks so much, I haded to put this in to my users_controller and then I just made a loop in order to post all of the recent properties. Thanks!! –  Jan 30 '20 at 18:54