Getting Super Confused with User associations in Rails -


i teaching myself rails , project working on auction site. user can either seller or bidder. when tried associations, can seller's information show not bidder's. have , still can't figure out. appreciated.

https://github.com/sgrzona/ebay

auction.rb  class auction < activerecord::base   belongs_to :seller, class_name: 'user', foreign_key: :user_id   has_many :auction_bids   belongs_to :bidder, class_name: 'user'  auction_bid.rb  class auctionbid < activerecord::base   belongs_to :bidder, :class_name => 'user', :foreign_key => 'bidder_id'   has_one :seller, :class_name => 'user'   belongs_to :auction  user.rb  class user < activerecord::base   devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable    has_many :auctions   has_many :auction_bids 

auction_bids_controller.rb

class auctionbidscontroller < applicationcontroller   before_filter :authenticate_user!, only: [:new, :edit, :update, :create, :destroy]    def     @auction_bids = current_user.auction_bids.order("expires_at asc")   end    def index     @auction = auction.find(params[:auction_id])     @auction_bids = @auction.auction_bids.order("bid desc")   end    def new     @auction = auction.find(params[:auction_id])     @auction_bid = auctionbid.new   end    def show     @auction_bid = auctionbid.find(params[:id])     @auction = @auction_bid.auction   end    def create     @auction = auction.find(params[:auction_id])     @auction_bid = @auction.auction_bids.new(auction_bid_params)      rails.logger.info "\n\n*** #{@auction_bid.inspect}\n"      if not_users_own_auction?       if @auction_bid.save         redirect_to @auction, :notice => "you current high bidder"       else         render :new       end     else       flash[:error] = "you cannot bid on won auction."       redirect_to @auction     end   end     private    def auction_bid_params     params.require(:auction_bid).permit(:bid)   end    def not_users_own_auction?     @auction.seller != current_user   end end 

auctions_controller.rb

class auctionscontroller < applicationcontroller   before_filter :authenticate_user!, only: [:new, :edit, :update, :create, :destroy, :my]   before_action :set_auction, only: [:show, :edit, :update, :destroy]   before_action :correct_user, only: [:edit, :update, :destroy ]     # /auctions   def index     @auctions = auction.all   end    def     @auctions = current_user.auctions.order("expires_at asc")     @auction_bids = current_user.auction_bids.order("expires_at asc")   end     # /auctions/1   def show     @auction = auction.find(params[:id])   end    # /auctions/new   def new     @auction = current_user.auctions.build   end    # post /auctions   def create       @auction = current_user.auctions.build(auction_params)      if @auction.save       redirect_to @auction, notice: 'auction created.'      else       render action: "new"     end   end    def update     if @auction.update(auction_params)       redirect_to @auction, notice: 'auction   updated.'      else       render action: 'edit'     end   end     # delete /auctions/1   def destroy     if @auction.destroy       redirect_to auctions_url, notice: 'auction destroyed.'      else       redirect_to auctions_url, error: 'auction not destroyed.'     end   end     private     # use callbacks share common setup or constraints between actions.     def set_auction       @auction = auction.find(params[:id])     end      def correct_user       if @auction.seller != current_user         redirect_to auctions_path, error: "not authorized edit auction"        end     end     # never trust parameters scary internet, allow white list through.     def auction_params       params.require(:auction).permit(:title, :description, :user_id, :image, :expires_at)     end end 

i think you're looking associations along lines of:

class auction < activerecord::base   belongs_to :seller, class_name: 'user', foreign_key: :user_id   has_many :auction_bids   has_many :bidders, :through => :auction_bids  auction_bid.rb  class auctionbid < activerecord::base   belongs_to :bidder, :class_name => 'user', :foreign_key => :bidder_id   belongs_to :auction  user.rb  class user < activerecord::base   devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable    has_many :auctions    , :through => :auction_bids   has_many :auction_bids, :foreign_key => 'bidder_id' 

@auction.bidders collection proxy. http://apidock.com/rails/activerecord/associations/collectionproxy. rails returning set of bidder objects you have iterate on like:

@auction.bidders.each |bidder|   puts bidder.to_s end 

or can like:

@auction.bidders.count 

... number of bidders example.


Popular posts from this blog