Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从代码中生成Rails模型(从控制器调用生成器)_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 从代码中生成Rails模型(从控制器调用生成器)

Ruby on rails 从代码中生成Rails模型(从控制器调用生成器),ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我特别需要能够从代码中调用Rails命令(即当某些操作发生时)。我需要创建一个带有特定字段(由表单确定)的模型,并在最后运行创建的迁移 因此,我使用的这个表单将创建所有字段,然后创建一个带有特定字段(表和列)的模型 那么,有没有一种方法可以从控制器/ruby代码中调用rails生成模型名[field[:type][:index]field[:type]和bundle exec rake db:migrate create table category ( id serial primar

我特别需要能够从代码中调用Rails命令(即当某些操作发生时)。我需要创建一个带有特定字段(由表单确定)的模型,并在最后运行创建的迁移

因此,我使用的这个表单将创建所有字段,然后创建一个带有特定字段(表和列)的模型


那么,有没有一种方法可以从控制器/ruby代码中调用
rails生成模型名[field[:type][:index]field[:type]
bundle exec rake db:migrate

create table category (
    id serial primary key,
    name text not null
);

create table attribute (
    id serial primary key,
    name text not null
);

create table item (
    id serial primary key,
    category_id integer not null references category (id),
    description text
);

create table category_attribute (
    attribute_id integer not null references attribute (id),
    category_id integer not null references category (id)
);

create table item_attribute (
    attribute_id integer not null references (attribute.id),
    item_id integer not null references item (id),
    value text
);
创建类别时,将其名称(以及任何其他一对一属性)存储在
类别
表中。确保
属性
表中包含该类别所具有的每个属性的条目,然后使用
类别属性
表将这些属性链接到该类别

当您添加一个类别的新成员时,您可以使用
item
表来存储关于该项目的主要内容,并使用
item\u属性
表来存储其每个属性的值

category
 id | name
----+------
  1 | car
  2 | pet

attribute
 id |    name
----+------------
  1 | make
  2 | breed
  3 | model_year
  4 | name

category_attribute
 attribute_id | category_id
--------------+-------------
            1 |           1
            2 |           2
            3 |           1
            4 |           2

item
 id | category_id |  description
----+-------------+----------------
  1 |           1 | Hyundai Accent
  2 |           2 | Fuzzy kitty

item_attribute
 attribute_id | item_id |  value
--------------+---------+---------
            1 |       1 | Hyundai
            3 |       1 | 2007
            2 |       2 | DSH
            4 |       2 | Sam

这种方法可能感觉很不明显,因为它与“一个对象具有多个属性”不匹配您在Rails模型中使用的样式。然而,这就是关系数据库的工作方式。我相信您可以使用一些ActiveRecord魔术来使对象/关系转换更加自动化,但我不记得它目前的名称。

为什么您有这种需要?我问,因为这听起来很有问题,而且可能有问题实现同样目标的更好方法。是的,也许我的解决方案有点过于极端。我需要实现的是:用自己指定的信息字段创建多个类别(即汽车与宠物有不同的字段)一旦创建了这样一个类别,我需要的命令将被调用,并且为每个类别创建一个新表。我知道我可以将所有字段存储为某种字符串,然后对其进行处理以正确显示,但我的web应用程序需要一个高级搜索功能,为每个类别创建单独的表似乎是最好的实现方法我真的很想听听其他解决方案。我肯定会寻求另一种解决方案——根据您的需要,有许多潜在的解决方案——可能是Rails
serialize
,或者Postgres hstore,或者像
category\u fields
这样的单独表格。在生产中生成模型和迁移似乎是一个痛苦的世界建议发布另一个问题,说明您试图解决的问题和限制条件(例如,提供有关您搜索需求的更多详细信息)我相信很多人都会有好的想法。我在这里问了另一个问题…问题是分类将在管理中创建,并且使用此功能时会非常谨慎。我非常感谢您对我新创建的问题的意见。我喜欢这种方法,但我不确定如何在Rails中实现这一点。嗯,y你可以从创建这些表和模型开始,并在模型的帮助器方法中手动进行链接。一旦你有了一个工作,就可以重新学习ActiveRecord中可用的关联方法,以将其清理干净并使其变得更好。