【问题标题】:A novice needs some help setting up relations between tables新手需要一些帮助来建立表之间的关系
【发布时间】:2015-07-09 09:13:18
【问题描述】:

大家好,我是 ruby​​ 新手,我需要一些帮助: 我在 ruby​​ 中设置了三个表,以下是模型:

班级用户:

 coding: utf-8

class User < ActiveRecord::Base
    validates :password, presence: true, confirmation: {strict: true}
    validates :password_confirmation, presence: true
    validates :telephone, uniqueness: true, presence: true, numericality: { only_integer: true }, presence: true, length: { minimum: 9, maximum: 9 }
    validates :name, presence: true, length: { minimum: 4, maximum: 30 }, format: { with: /^[\w\s-]*/u, multiline: true,
                                                                              message: 'only allows letters' }
   has_many :users_valorations
   has_many :valoraions, through: :users_valorations
end

班级评估:

class Valoration < ActiveRecord::Base
   validates :points, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
   validates :category, presence: true, length: { minimum: 4, maximum: 30 }, format: { with: /\A[a-zA-Z\ ]+\z/,
                                                                              message: 'only allows letters' }
   has_many :users_valorations
   has_many :users, through: :users_valorations
end

Class UsersValoraion

class UsersValoration < ActiveRecord::Base
   belongs_to :user
   belongs_to :valoration
end

schema.rb:

  ActiveRecord::Schema.define(version: 20150708212501) do

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.integer  "telephone"
    t.string   "password"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users_valorations", force: :cascade do |t|
    t.integer  "valoration_sender_id"
    t.integer  "valoration_target_id"
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
  end

  add_index "users_valorations", ["valoration_sender_id"], name: "index_users_valorations_on_valoration_sender_id"
  add_index "users_valorations", ["valoration_target_id"], name: "index_users_valorations_on_valoration_target_id"

  create_table "valorations", force: :cascade do |t|
    t.integer  "points"
    t.string   "category"
    t.datetime "time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

所有的类都是由脚手架生成的,现在我只想做这个:

->当您创建评估时,您必须选择谁发送它以及谁将接收它,并且该数据将保存在 UsersValoration 表中 ->列出所有的评价,并附上发送它们的用户的链接。

我是新手,欢迎大家帮忙

【问题讨论】:

  • 在 users_valorations 表中,添加 user_id 和 valoraion_id 列以使其工作。

标签: ruby-on-rails ruby database


【解决方案1】:

据我了解,您需要在创建 UserValoration 之前或之后进行操作。

ActiveRecord 提供回调以在对象生命周期中执行某些操作。您可以在这里找到更多信息:http://guides.rubyonrails.org/active_record_callbacks.html

【讨论】:

  • Thaks,我会看的,但是关系稳定了吗?我需要在 UsersGeneration 模型中设置 foreign_key 选项吗?
  • 不,你没有。您可能需要做的事情是在 users_valorations 表中添加一个名为 type 的列,以指示用户是发送者还是接收者。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 2011-08-07
  • 2013-11-27
  • 1970-01-01
  • 2021-01-24
  • 2012-07-11
  • 1970-01-01
相关资源
最近更新 更多