简体   繁体   中英

How to favorite user profiles?

So, I have an application with many users, that have user profiles. I'd like to be able to Favorite a user profile, which is added to my favorites tab. I've written the below code but I'm getting this error:

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "favorites_users" does not exist
2014-02-04T02:18:56.217171+00:00 app[web.1]: LINE 5:                WHERE a.attrelid = '"favorites_users"'::regcl...
                             ^

Right now I have the following code:

favorite.rb

class Favorite < ActiveRecord::Base
    has_and_belongs_to_many :users
end

favorites_controller.rb

class FavoritesController < ApplicationController
 before_filter :find_user

  def index
    @favorites = @user.favorites
  end

  def create
    @user = User.find params[:id]
    @user.favorites << @user
  def

  def destroy
    @favorite = @user.favorites.find_by_user_id params[:id]
    @favorite.destroy unless @favorite.blank?
  end
  end

  private

  def find_user
    @user = current_user
  end

end

favorites.html.erb

<div class="row">
  <%= render 'layouts/left_side' %>
  <div role="main" class="col-md-6">
    <div class="visitors-m">
      <div class="col-md-12 showing">
        <div class="row">
          <div class="col-md-12">
            <h4><%= pluralize(@favorites.size, 'favorites') %></h4>
            <div class="col-md-3 row fr">
              <select class="form-control">
                <option value="">SORT BY</option>
                <option value="1">General</option>
                <option value="2">News</option>
                <option value="3">Media</option>
                <option value="4">Funny</option>
              </select>
            </div>
          </div>
        </div>
      </div>


      <%#= @favorites.map(&:name).to_sentence %>


      <% @favorites.each do |favorite| %>
      <div class="col-md-12 users_info visitors">
        <div class="row">
          <div class="col-md-12">
            <div class="img-thumbnail">
              <% if favorite.profile_image.present? %>
                <%= image_tag(favorite.profile_image_url(:thumb), size: '100x100') %>
              <% else %>
                <%= image_tag("people_image.jpg") %>
              <% end %>
            </div>
            <div class="users_about_info">
              <h4><%= visitor.name %></h4>
              <p class="visit-time">
                <%= timeago_tag(current_user.visits.where(favorite: favorite).first.visited_at) %>
              </p>
            </div>
            <div class="fr">
              <p><%= favorite.address %></p>
              <p><%= favorite.age %> years old</p>
            </div>
            <div class="clr"></div>
          </div>
        </div>
      </div>
      <% end %>
    </div>
  </div>
  <%= render 'users/visitors_filter' %>
</div>

favorites migration

class CreateFavorites < ActiveRecord::Migration
  def change
    create_table :favorites do |t|

      t.timestamps
    end
  end
end

favorites_users is not a column under either favourite or users, naturally you will get that error. You need to call:

User.find(1).favourites

or

Favourite.find(1).users

Your implementation of favourites is alright assuming you have only 1 favourite ( the grammatical use of the word favourite. which unfortunately isn't widely used( most sites allow for more than 1 favourited item, or in your case, user. If the user can favourite more than 1 user profile then you need favourite relationships.

Let me know if you need help implementing that.

EDIT

Model

class User <ActiveRecord::Base
has_many :favourite_relationships
has_many :favourites, through: :favourite_relationships
has_many :people_who_favourited_me, through: :favourite_relationships, foreign_key: "favourite_id"
end

class FavouriteRelationships <ActiveRecord::Base
# attributes are ( user_id and favourite_id )
belongs_to :favourite
belongs_to :user
end

class Favourite < ActiveRecord::Base
has_many :favourite_relationships
has_many :users, through: :favourite_relationships
end

Kindly pass the user id as the current user id and the favourite id as the parameters to create a favourite.

<% form_for Favourite.new do |f| %> 
 <%= f.hidden_field :user_id, value: current_user.id %>
etc....

and create the favourite with that form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM