rails:为模型创建脚手架以从超类继承吗? [英] rails: create scaffold for models to inherit from superclass?

查看:78
本文介绍了rails:为模型创建脚手架以从超类继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,仍然会弄湿我的脚,所以如果这是琐碎的事情或做事的错误方式",请原谅我.

I'm new to Rails, still getting my feet wet, so please pardon me if this is either trivial or "the wrong way" to do things.

我想为某些脚手架模型创建一个超类.例如,我想为MenWomen创建一个支架,但是我希望它们都从People超类继承. MenWomen将从People类继承诸如heightweight之类的字段.

I'd like to create a superclass for some scaffolded models. For example, I'd like to create a scaffold for Men and for Women, but I want them both to inherit from a People superclass; Men and Women would inherit fields like height and weight from the People class.

在哪里/如何定义此People超类?如何通过脚手架定义子类Women?

Where/how do I define this People superclass? How do I define the subclasses Men and Women via scaffolding?

推荐答案

这是我考虑过要对应用程序进行处理的事情.我还没有做过,如果您是Rails的新手,我也不会推荐它.我要么完全制作单独的模型,要么制作一个模型,并使用属性gender(应为0或1),然后创建一个返回对应性别字符串的方法.

This is something I've thought about doing with my application. I haven't done it yet, and I wouldn't recommend it if you are new to rails. I would either make separate models entirely, or make one model, and have the attribute gender, which should be either a 0 or a 1, and then make a method that returns the string for the corresponding gender.

编辑

所以我打开了rails控制台,从我看到的情况来看,完全有可能,您需要做的就是声明类,如果要使用其他表,则set_table_name

So I opened up the rails console, and from what I could see, it is possible totally possible, all you need to do is declare the class, and if you want to use different tables, set_table_name

class This < That
  set_table_name :this
end

class There < This
   set_table_name :there
end

或者您可以使用一张桌子,但是如果您尝试保持DRY,我将使用两张桌子.

Or you could use one table, but if your trying to stay DRY, I would use two.

如果要使用脚手架生成器,则必须为想要查看的每个类(男人和女人)运行典型的rails g scaffold Men.生成的模型继承自ActiveRecord::Base类.继承标记是小于符号(<).

If you want to use the scaffold generator, you will have to run the typical rails g scaffold Men for each class you want views for (men and women). The model that this generates inherits from the ActiveRecord::Base class. The inheritance marker is the less than symbol (<).

# THESE WILL BE THE DEFAULT GENERATED MODELS
class Men < ActiveRecord::Base

end

class Women < ActiveRecord::Base

end

然后您将手动创建超类User

You will then manually create the super class User

class User < ActiveRecord::Base

end

,然后编辑MenWomen模型以从User

and then edit the Men and Women models to inherit from User

# men.rb
class Men < User
end

# women.rb
class Women < User
end

让我们说您想用一个表子类化,您可以纠正该表的迁移,然后将attr_accessible添加到适当的子类中.

lets say you wanted to subclass with one table, you could would right the migrations for that table, and then add the attr_accessible to the appropriate subclass.

attr_accessible是rails安全功能.它确定可以在批量分配中设置哪些属性.与安全性,网站等级等相关的任何内容都不应访问.

attr_accessible is a rails security feature. It determines which attributes may be set in mass assignment. Anything related to security, site rank, etc. should not be accessible.

示例:

attr_accessible :favorite_food, :interests, :password, :email # THIS IS GOOD

attr_accessible :admin, :has_access_to_missile_launch_codes # THIS IS BAD

因为有人可能会通过

params => { :man => { :admin => true }}

主要要点是,使用这些attr_accessible将确定哪种类型的用户可以设置什么.显然,您可以通过将共享功能放在超类中来进行干燥.希望这会有所帮助

The main point is that using these attr_accessible will determine which type of user can set what. Obviously you can DRY this up by putting shared features in the super-class. Hope this helps

您还应该阅读有关super关键字和self关键字的信息.如果您运行的是继承的设置,则最终将要使用这些设置.

You should also read about the super keyword, and the self keyword. If your running an inherited setup you will eventually want to use these.

这篇关于rails:为模型创建脚手架以从超类继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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