models

How can I iterate through all of the Models in my rails app?

做~自己de王妃 提交于 2019-12-09 05:30:27
问题 I would like to be able to iterate over and inspect all the models in my rails app. In pseudo-code it would look something like: rails_env.models.each do |model| associations = model.reflect_on_all_associations(:has_many) ... do some stuff end My question is how do I inspect my rails app to get a collection of the models (rails_env.models) ? 回答1: Similar to nathanvda's response, use camelize rather than capitalize to support model files with underscores, and use String#constantize rather than

Application / MVC Event Model

时间秒杀一切 提交于 2019-12-09 04:31:27
Update: This question was inspired by my larger quest for mapping ontologically the whole software systems architecture enchilada. I've written a blog post about it , and hopefully it will help clarify what I'm after. Many, many, many frameworks and stacks that's event-driven have too much variation for my little head to get around. Is there somewhere some resources that defines the outline of a reasonable Application Event Model, what events there are, and what triggers are most common? I've got my own framework with a plugin and event-driven architecture, but I want to open-source it, and as

django数据写入到数据库

时间秒杀一切 提交于 2019-12-08 23:01:55
1.在django中,一个类对应一个表。 定义表名: setting.py 中的DATABASES设置数据库的选用'ENGINE':'django.db.backends.mysql', 名字'NAME':'', 账号'USER':'', 密码'PASSWORD':'', 数据库的的服务器IP 'HOST':'mysql.。。。.com', 端口'PORT':'7150',端口号也是一个字符串,默认是3306,远程会为了安全会进行更改。 models里是用class进行创建的,一个类一个表。 2.所有的数据库的操作都在APP下的models.py文件中在model.py文件中创建模型: from _future_ import unicode-literals from django.db import models class PosModel(models.Model): models.Model继承models里的Model类,Model类是一个父类,本身基类是没有的。 username = models.CharField(max_length=16,null=False) 定义了一个字段类型,名字叫username,最大长度为16,不能为空。 password = models.CharField(max_length=32,null=False) age =

抽象工厂

情到浓时终转凉″ 提交于 2019-12-08 20:14:43
1,创建Models 2,创建业务接口IBusinessInterface----添加Models引用 3,创建业务实现BusinessClass1与BusinessClass2(注意:两组中的实现类要同名,但命名空间不可以相同)---添加Models和IBusinessInterface引用 4,创建工厂Factroy 5,Program中添加所有引用 Models代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models { public class Car { public string WheelName { get; set; } public string LightName { get; set; } } } Car using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models { public class Person { public string

How can I set user full name in foreignkey field with User Model using on_delete attribute?

纵饮孤独 提交于 2019-12-08 15:58:31
问题 I have a model in django that have foreignkey with User model. class News(models.Model): user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.SET(???)) message - models.TextField() So When any user account delete from database then in News table entries also deleted according to user. So I want that When any user account deleted then I need to set his/her full name in user field using 'on_delete=models.SET(???)' Example: If I have user that first_name = 'Neeraj' and last_name='Kumar'

How to use read_attribute when manually defining getter/setter (attr_accessor or attr_writers)

只愿长相守 提交于 2019-12-08 13:34:45
问题 I set up a search model and want to require at least one field is filled out. I found a question that helps with the validation, Rails: how to require at least one field not to be blank. (I tried all answers, but Voyta's seems the best.) The validation is working, except when I want to redefine the getter/setter via attr_accessor or attr_writer . (I have virtual attributes on the form that need to be apart of the validation.) To figure out what the problem is, I tested with an attribute that

replacing a file while uploading in django

六眼飞鱼酱① 提交于 2019-12-08 09:52:50
问题 I have the following code in my models.py: def upload_to_cars(instance, filename): blocks = filename.split('.') ext = blocks[-1] filename = "%s.%s" % (instance.name.replace(" ", "-"), ext) instance.title = blocks[0] return filename class Cars(models.Model): image_file = models.ImageField(upload_to=upload_to_cars, null=True, blank=True) name = models.CharField(max_length=200) When I upload a second image I want the first one to be deleted. So that there will always be only one image per car

Nodejs Mongoose render two models from collections

时光总嘲笑我的痴心妄想 提交于 2019-12-08 09:37:09
问题 I hope the heading describes my question right. When I open /admin/liste, it should render two tables from MongoDB database. How can I render two or more collections? app.get('/admin/liste', isLoggedIn, function(req, res) { var List1 = mongoose.model('List1'); // var List2 = mongoose.model('List2'); List1.find(function (err, docs) { res.render('admin/liste',{ firstlist : docs }); }); /*List2.find(function (err, docs) { res.render('admin/liste',{ seclist : docs }); });*/ }); EDIT: I can't find

Ruby on Rails: Relation between two models

喜欢而已 提交于 2019-12-08 05:09:52
问题 I am challenging myself to build a little marketplace where you will be able to post a "request" inside a category. For that I have both the request model and the category model. How can I add a relation between those models, so that the Category knows that it belongs to a request and vice versa? I already did: category.rb has_and_belongs_to_many :requests request.rb has_one :category Now inside my form partial I have this code: <%= f.select :category, Category.all, :prompt => "Kategorie",

cakePHP model associations (retrieving data deeper)

我怕爱的太早我们不能终老 提交于 2019-12-08 05:00:43
问题 I tried to find this everywhere but I cant. Here is my problem: Lets say I have models associated like this: Student (has many Memberships) Membership (belongs to Student and TeacherCourse) TeacherCourse (belongs to Teacher and Course) Teacher (has many TeacherCourses) Course (has many TeacherCourses) When I use membershipsController to send all membership to the view, I get only for example $membership['Membership']['id'] or $membership['TeacherCourse']['id'] BUT NOT $membership[