- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
如果有更有效的方法,我希望确认以下关联声明是否有效。
我有一个动物模型,您可以在其中创建狗、猫、兔子等,我还需要指定动物的品种,所以我想为每种动元素种类型建立一个模型,例如 DogBreed 和然后是 Cat Breed。
我在想每只动物只能有一个品种,这样的事情也行
class Animal
has_one :dog_breed
has_one :cat_breed
end
class DogBreed
belongs_to :animal
end
class CatBreed
belongs_to :animal
end
每个模型的列是
Animal
name
description
size
breed
DogBreed
name
CatBreed
name
有没有更好的方法来解决这个问题?
我还将为每个品种模型的动物模型添加 accepts_nested_attributes_for
谢谢
最佳答案
性传播感染
您正在寻找 Single Table Inheritance
:
#app/models/animal.rb
class Animal < ActiveRecord::Base
has_many :x
end
#app/models/dog.rb
class Dog < Animal
end
#app/models/cat.rb
class Cat < Animal
end
正如名称“单表继承”所暗示的那样,您的“依赖”模型将继承自。这意味着您将能够存储一个名为 animals
的中央表,您需要在其中添加一个type
列:
$ rails g migration AddTypeToAnimals
#db/migrate/add_type_to_animals.rb
class AddTypeToAnimals
def change
add_column :animals, :type, :string
end
end
--
修复
这个工作的方式其实很简单。
您可以毫无顾忌地调用您的Dog
和Cat
模型(没有超出“正常”Rails 工作范围的更改)。 type
列将自动填充:
#app/controllers/dogs_controller.b
class DogsController < ApplicationController
def new
@owner_dog = Dog.new
end
def create
@owner_dog = Dog.new dog_params
@owner_dog.save
end
private
def dog_params
params.require(:dog).permit(:x,:y,:z)
end
end
更新
在我们的 Skype 谈话中,您可能想要这样做:
#app/models/animal.rb
class Animal < ActiveRecord::Base
#fields id | breed_id | name | created_at | updated_at
belongs_to :breed
delegate :name, to: :breed, prefix: true
end
#app/models/breed.rb
class Breed < ActiveRecord::Base
#fields id | name | created_at | updated_at
has_many :animals
end
这将使您能够使用以下内容:
#app/controllers/animals_controller.rb
class AnimalsController < ApplicationController
def new
@animal = Animal.new
end
def create
@animal = Animal.new animal_params
end
private
def animal_params
params.require(:animal).permit(:name, :breed_id)
end
end
#app/views/animals/new.html.erb
<%= form_for @animal do |f| %>
<%= f.text_field :name %>
<%= f.collection_select :breed_id, Breed.all, :id, :name %>
<%= f.submit %>
<% end %>
关于ruby-on-rails - Rails 协会澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25805972/
您好,我有一个用 cakephp 开发的网站。我有两张 table :成分属性 表之间的关系是 HasAndBelongsToMany(HABTM)。这是我的模型: class Ingredient
我从 sails.js 开始,我完全迷失了我的 sql 查询。 我有以下表格: genres +-----------+--------------+------+-----+ | Field
我正在开发一个 Rails 应用程序,用户可以在其中将他们希望执行的任务添加到他们自己的自定义列表中。每个任务也可以属于 0 个或多个类别。到目前为止,我已经试过了: 用户.rb has_one :u
我是新手。我正在尝试加入不同的集合以获取一些数据。我能够获取帖子创建者和喜欢的信息,但是如何使用 mongoose 中的关联获取包含他们的评论和评论者信息的帖子? 用户模式 const userSc
我试图让 mongoid 保存关联,但我只能让一侧工作。如果我有以下测试。 test "should add a user as a follower when a user follows th
我正在为 has_many 使用 FactoryGirl 示例来自 http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-gir
我想为我的应用程序实现一个消息传递系统。 我有用户。 我应该怎么做? 创建一个带有外部两个用户外键的消息模型??.. 完成这项工作的最合适的方法是什么? 我担心的是,如果我查询“message.use
我正在尝试使用 FactoryGirl 创建一个测试数据库,该数据库的关联是 Parent has_many Entries。此时抛出Parent不能为空的ActiveRecord验证错误。我在这方面
我有以下关联 class Training 我也试过 FactoryGirl.define do factory :attendance do training associat
以下是我的制造商: Fabricator(:my_fabricator) do my_first_association my_second_association end 这里的问题是我需要
有关 Rails 关联的问题。 考虑以下模型: 人 事件 图片 人物和事件可以有很多图像。 上传到事件的图像需要具有与多人关联的能力。 这意味着人和图像之间存在两种关系。 一种将图像直接上传到人物身上
谁能告诉我 Android 应用程序开发与 Java 的关联有多密切。也就是说,从事 Android 开发的人员有可能从事 Core Java 或 J2EE 或 J2SE 的工作吗? 让我把我的问题说
从 Rails 4 和 Postgres 9 开始,引入了 array column。我还尝试使用数组列来存储诸如 tags 或 categories 之类的东西,我不经常更改它们,它用作搜索的引用/
我正在尝试从一对多关系创建一个序列化的 RESTful 响应。 我有一张表Lexicon,主键lexicon_id,我还有一张表lexicon_attributes,主键lexicon_attribu
协会的确切目的是什么?我了解关系的含义以及何时使用每种类型,例如: belongs_to, has_many, has_one , has_and_belongs_to_many, ect 但我不太明
最近我开始使用 Ruby on Rails 而不是 PHP 创建我的网站。 我已经很容易地掌握了这门语言,但仍然对联想没有 100% 的信心:) 我有这种情况: 用户模型 has_and_belong
我想用Sinatra和DataMapper做一个博客应用,我的主应用文件是这样的。 %w[rubygems sinatra data_mapper].each{ |r| require r } Dat
我有一个以前用于生产的 MySQL 数据库,现在我们的团队正在迁移到 SailsJS。我读过有关 sails 船协会的资料,我认为它很棒。但我想知道您是否可以使用关联来使用 .populate 方法填
我有一个模型 ShippingOption,它有_many ShippingSpeedOptions 还有一个要求: ShippingOption 必须有至少一个 ShippingSpeedOptio
好的,这是我的问题。我有 3 种不同的模型,人员、角色、客户和商店。客户有很多商店,也可以有很多人。商店有很多人。人有不同的角色。 1 人可以在多家门店工作,在每家门店可能担任不同的角色。 例如。 J
我是一名优秀的程序员,十分优秀!