协会用的MongoDB数据库轨 [英] Association in rails using mongodb as database

查看:187
本文介绍了协会用的MongoDB数据库轨的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的设计。它给当前的userid

  current_user.id

有很多用户。有一个控制器名称empsals_controller.rb

 类EmpsalsController< ApplicationController中  高清指数
    @empsals = Empsal.all
  结束  高清节目
    @empsal = Empsal.find(PARAMS [:ID])  结束  高清新
    @empsal = Empsal.new
  结束
  高清编辑
    @empsal = Empsal.find(PARAMS [:ID])
  结束  打造高清
    @empsal = Empsal.new(PARAMS [:empsal])    做的respond_to |格式|
      如果@ empsal.save
        format.html {redirect_to的@empsal,通知:Empsal创建成功。 }
        format.json {渲染JSON:@empsal,状态:创建,地点:@empsal}
      其他
        format.html {渲染动作:新}
        format.json {渲染JSON:@ empsal.errors,状态:unprocessable_entity}
      结束
    结束
  结束  DEF更新
    @empsal = Empsal.find(PARAMS [:ID])    做的respond_to |格式|
      如果@ empsal.update_attributes(PARAMS [:empsal])
        format.html {redirect_to的@empsal,通知:Empsal已成功更新。 }
        format.json {头:NO_CONTENT}
      其他
        format.html {渲染动作:编辑}
        format.json {渲染JSON:@ empsal.errors,状态:unprocessable_entity}
      结束
    结束
  结束  DEF破坏
    @empsal = Empsal.find(PARAMS [:ID])
    @ empsal.destroy    做的respond_to |格式|
      format.html {redirect_to的empsals_url}
      format.json {头:NO_CONTENT}
    结束
  结束

该控制器的型号为

 类Empsal
  包括Mongoid ::文件
   belongs_to的:pay​​grade
  现场:salary_component,类型:字符串
  现场:pay_frequency,类型:字符串
  现场:货币类型:字符串
  现场:数量,类型:字符串
  现场:意见类型:字符串
 validates_ presence_of:pay_frequency结束

我想和具有模型user.rb,使得相关的用户可以查看他们的相关数据色器件关联。

 类用户
  包括Mongoid ::文件
  包括Mongoid ::时间戳
设计:database_authenticatable,:可注册,#:可证实,
         :可恢复的,:rememberable,:可追踪,:可验证的,:timeoutable,:timeout_in => 2分钟
   现场:角色
结束


解决方案

您有你需要的,除了在用户模式设置负相关的一切:

 类用户
  包括Mongoid ::文件
  包括Mongoid ::时间戳  的has_many:empsals#<<<<<<<加行  设计:database_authenticatable,:可注册,#:可证实,
         :可恢复的,:rememberable,:可追踪,:可验证的,:timeoutable,:timeout_in => 2分钟
   现场:角色
结束

查看文档的 http://mongoid.org/en/mongoid/文档/ relations.html#的has_many

通过这个,你可以做这样的事情。

  @ user.empsals#这将是Empsal实例的列表

I am using devise. It gives the current userid as

current_user.id

There are many users. There is a controller name as empsals_controller.rb

class EmpsalsController < ApplicationController

  def index
    @empsals = Empsal.all


  end

  def show
    @empsal = Empsal.find(params[:id])

  end

  def new
    @empsal = Empsal.new


  end


  def edit
    @empsal = Empsal.find(params[:id])
  end

  def create
    @empsal = Empsal.new(params[:empsal])

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

  def update
    @empsal = Empsal.find(params[:id])

    respond_to do |format|
      if @empsal.update_attributes(params[:empsal])
        format.html { redirect_to @empsal, notice: 'Empsal was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @empsal.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @empsal = Empsal.find(params[:id])
    @empsal.destroy

    respond_to do |format|
      format.html { redirect_to empsals_url }
      format.json { head :no_content }
    end
  end

The model of this controller is

class Empsal
  include Mongoid::Document
   belongs_to :paygrade
  field :salary_component, type: String
  field :pay_frequency, type: String
  field :currency, type: String
  field :amount, type: String
  field :comments, type: String
 validates_presence_of :pay_frequency

end

I want to make association with devise which have model user.rb such that related user can view their related data.

class User
  include Mongoid::Document
  include Mongoid::Timestamps
devise :database_authenticatable, :registerable, #:confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable, :timeout_in => 2.minutes
   field :role
end

解决方案

You have everything you need, except for setting the inverse association in the User model:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  has_many :empsals # <<<<<<< added line

  devise :database_authenticatable, :registerable, #:confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :timeoutable, :timeout_in => 2.minutes
   field :role
end

See the documentation at http://mongoid.org/en/mongoid/docs/relations.html#has_many

With this, you can do things like

@user.empsals # it will be a list of Empsal instances

这篇关于协会用的MongoDB数据库轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆