Rails 4多态关联has_many

时间:2013-12-17 23:09:08

标签: ruby-on-rails ruby activerecord ruby-on-rails-4

有没有办法在Rails 4中使用has_manypolymorphic关联?

以下是我的booth型号

has_many :chat, as: :chattable, dependent: :destroy
    accepts_nested_attributes_for :chat

以下是我的chat型号

class Chat < ActiveRecord::Base
    belongs_to :from_user, class_name: 'User'
    belongs_to :to_user, class_name: 'User'
    belongs_to :chattable, polymorphic: true
end

我似乎无法在我的booth控制器和视图中检索chattable

我试过

@booth.chat

但它说它是未定义的方法

Booths控制器

class BoothsController < ApplicationController
    load_and_authorize_resource
    before_filter :authenticate_user!
    before_action :set_booth, only: [:show, :edit, :update, :destroy, :about, :products, :literature, :partners, :presentations, :contact, :videos]
    before_action :sidebar_menu

    layout "users"

    def index
        @booths = Booth.all
    end

    def show
        @booth.build_chats
        hide_sidebar 'hide-sidebar'
        @menu_items << [edit_booth_path(@booth), "Edit this booth"]
        add_sidebar_menu ["Directory", "home"], "home-nav", 
            [[hall_visit_path(@booth.hall), "Visit hall"]], false
        respond_to do |format|
            format.html
            format.js
        end
    end

    def new
        @booth = Booth.new
        @booth.build_uploaded_file
    end

    def edit
        @menu_items << [booth_path(@booth), "Show this booth"]
        @booth.build_uploaded_file unless @booth.uploaded_file
    end

    def create
        @booth = Booth.new(booth_params)

        respond_to do |format|
          if @booth.save
            format.html { redirect_to @booth, notice: 'Booth was successfully created.' }
            format.json { render action: 'show', status: :created, location: @booth }
          else
            format.html { render action: 'new' }
            format.json { render json: @booth.errors, status: :unprocessable_entity }
          end
        end
    end

    def update
        if params[:booth][:uploaded_file_attributes][:assets].blank?
            params[:booth].delete(:uploaded_file_attributes)
        end
        respond_to do |format|
          if @booth.update(booth_params)
            sync_update @booth
            format.html { redirect_to @booth, notice: 'Booth was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render action: 'edit' }
            format.json { render json: @booth.errors, status: :unprocessable_entity }
          end
        end
    end

      def destroy
        @booth.destroy
        respond_to do |format|
          format.html { redirect_to booths_url }
          format.json { head :no_content }
        end
      end


    def about
        render layout: false
    end

    def products
        render layout: false
    end

    def literature
        render layout: false
    end

    def partners
        render layout: false
    end

    def presentations
        render layout: false
    end

    def contact
        render layout: false
    end

    def videos
        render layout: false
    end

    def chat_widget

    end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_booth
          @booth = Booth.find(params[:id])
          uploaded_file_url(@booth)
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def booth_params
          params.require(:booth).permit(:name, :company_website, :social_media, :contact_info, :email, :about_us, :greeting_type, :event_id, :public_chat, :twitter_roll, :twitter_hash_tag, :survey_url, :prize_giveaway_description, :newsletter_description, :greeting_image_id, :greeting_audio_id, :greeting_video_id, :greeting_virtual_id, :user_id, :booth_package, :display_mode, :greeting_video,
            :facebook_url, :linkedin_url, :twitter_url, :top_message, :template_id, :hall_id, uploaded_file_attributes: [:assets])
        end

        def uploaded_file_url(booth)
            @booth_logo_url = booth.uploaded_file.assets.url if booth.uploaded_file
        end

        def sidebar_menu
            super ["Booths", "suitcase"], "booth-nav", [[booths_path, "List booths"], [new_booth_path, "Create a booth"]], true
        end
end

1 个答案:

答案 0 :(得分:2)

我看到的唯一问题是你的booth模式:

has_many :chat, as: :chattable, dependent: :destroy

确保在使用chats时使用复数has_many。尝试用以下代码替换该行:

has_many :chats, as: :chattable, dependent: :destroy

然后你会参考展位的聊天:@booth.chats