我是 Rails 初学者,我面临一个基本问题。我找到了几个解决方案,但没有一个能够完全描述我的问题。我有两个模型:联系人和组
Group has_many :contacts
Contact belongs_to :group
Run Code Online (Sandbox Code Playgroud)
(通过devise还有第三种用户模型)
创建新联系人时,我希望用户能够通过选择选项(如下拉字段)选择该联系人所属的组。
我在联系人表中添加了一个 :group_id 索引:
class AddGroupIdToContacts < ActiveRecord::Migration
def change
add_column :contacts, :group_id, :integer
end
end
Run Code Online (Sandbox Code Playgroud)
在 view/contacts/_form 中,我使用了 collection_select 选项来输出显示可用组的下拉字段:
<%= form_for(@contact) do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% @contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
...
<div class="field">
<%= f.label :group_id %><br>
<%= f.collection_select(:group_id, Group.all, :id, …
Run Code Online (Sandbox Code Playgroud) 我有一个网站,允许用户通过多种服务(LinkedIn、电子邮件、Twitter 等)登录。
我设置了以下结构来建模 aUser
及其多重身份。基本上,一个用户可以有多个身份,但只能是给定类型的一个(例如不能有 2 个 Twitter 身份)。
我决定将其设置为多态关系,如下所示。基本上有一个中间表identities
将一个条目映射User
到多个*_identity
表。
关联如下(仅显示LinkedInIdentity
,但可以推断)
# /app/models/user.rb
class User < ActiveRecord::Base
has_many :identities
has_one :linkedin_identity, through: :identity, source: :identity, source_type: "LinkedinIdentity"
...
end
# /app/models/identity
class Identity < ActiveRecord::Base
belongs_to :user
belongs_to :identity, polymorphic: true
...
end
# /app/models/linkedin_identity.rb
class LinkedinIdentity < ActiveRecord::Base
has_one :identity, as: :identity
has_one :user, through: :identity
...
end
Run Code Online (Sandbox Code Playgroud)
我遇到的问题与User
模型有关。由于它可以有多个身份,因此我使用has_many :identities
. 但是,对于给定的身份类型(例如 LinkedIn),我使用了has_one :linkedin_identity ...
. …
我有模特
class Question(models.Model):
question = models.CharField(max_length="200")
class Answer(models.Model):
question= models.ForeignKey(Question)
Run Code Online (Sandbox Code Playgroud)
所以,我想要我在Answers中没有的所有问题
我在回答中有
Question 1
Question 2
Run Code Online (Sandbox Code Playgroud)
在问题中我有
Question 1
Question 2
Question 3
Question 4
Run Code Online (Sandbox Code Playgroud)
我想要查询问题3和问题4的结果
谢谢
我的这个symfony Recepciones类与Obras_Sociales_Recepciones类有关,因为我的BaseRecepciones类声称:
/* @method Recepciones setRecepcionId() Sets the current record's "recepcion_id" value
* @method Recepciones setCuentaLaboratorioId() Sets the current record's "cuenta_laboratorio_id" value
* @method Recepciones setCuentasLaboratorios() Sets the current record's "Cuentas_Laboratorios" value
* @method Recepciones setObrasSocialesRecepciones() Sets the current record's "Obras_Sociales_Recepciones" collection
*/
abstract class BaseRecepciones extends sfDoctrineRecord
{
public function setTableDefinition()
{
$this->setTableName('Recepciones');
$this->hasColumn('recepcion_id', 'integer', 4, array(
'type' => 'integer',
'fixed' => 0,
'unsigned' => true,
'primary' => true,
'autoincrement' => true,
'length' => 4,
));
$this->hasColumn('cuenta_laboratorio_id', 'integer', 4, …
Run Code Online (Sandbox Code Playgroud) 我一直在研究一些"落后"关系的模型.我开始想知道......如果我想过滤一些结果怎么办?
我有两个型号:
Model A:
name
image
date
Model B:
ForeignKey to Model A
name
date
Run Code Online (Sandbox Code Playgroud)
要访问模型AI,请使用:p = A.objects.all().order_by(' - date')
然后我把它传递给模板.
在模板中:
{% for n in model_a.all %}
{% for item in n.modelb_set.all %}
<li>{{item.name}}</li>
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我得到了模型A的所有后向相关对象,那很好.
现在如果我想从该查询中过滤一些结果怎么办?
我在考虑通过以下方式迭代:
p = A.objects.all().order_by('-date')
for n in p:
for x in n.modelb_set.filter(date_lte=""):
ls = []
ls.append[x]
Run Code Online (Sandbox Code Playgroud)
我如何将其传递给模板并获得我之前获得的(未过滤的视图)但现在已经过滤了结果?
我建立了一个search
模型,并且要求至少填写一个字段.我发现了一个有助于验证的问题,Rails:如何要求至少一个字段不为空.(我尝试了所有答案,但Voyta似乎是最好的.)
验证工作正常,除非我想通过attr_accessor
或重新定义getter/setter attr_writer
.(我在表单上有虚拟属性需要与验证分开.)为了弄清问题是什么,我使用属性作为常规属性进行了测试item_length
.如果我添加attr_accessor :item_length
,验证将停止工作.所以,我想问题是如何在不使用点表示法的情况下读取属性的值.由于验证使用字符串,我无法使用正常的阅读方式.
这是一个片段:
if %w(keywords
item_length
item_length_feet
item_length_inches).all?{|attr| read_attribute(attr).blank?}
errors.add(:base, "Please fill out at least one field")
end
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,虚拟attrbutes(length_inches和length_feet)根本不起作用,并且普通属性(length)有效,除非我重新定义了getter/setter.
所以我在我的一个模型中创建了一个方法来解析文本体中的主题标签.这是模型:
class Conversation < ActiveRecord::Base
has_many :comments
has_many :hashtags, as: :hashtaggable, autosave: true
belongs_to :user
attr_accessible :description, :title, :user_id
before_save :create_hashtags
private
def create_hashtags
if self.description_changed? || self.hashtags.empty?
##scan for new hashtags
scanned_tags = self.description.scan(/#\w+/)
##delete old hashtag
self.hashtags.destroy_all unless self.hashtags.empty?
##save the new hashtag
scanned_tags.each do |hashtag|
self.hashtags.create name: hashtag
end unless scanned_tags.empty?
end
end
validates :description, presence: true, length: { in: 5..400 }
validates :title, presence: true, length: {in: 20..250}
validates :user, presence: true
end
Run Code Online (Sandbox Code Playgroud)
如果我正在更新已经存在的"对话",这可以正常工作,但如果我尝试创建一个新对话,我会收到此错误:
ActiveRecord::RecordNotSaved at …
Run Code Online (Sandbox Code Playgroud) 是否可以在laravel模型中使用继承?我解释:
是否可以执行一个扩展雄辩类的模型?
class A extends Eloquent
{
}
class B extends A
{
}
Run Code Online (Sandbox Code Playgroud)
e B也是2个不同的表,B有A_id作为外键和其他字段.怎么可能是B级的构造函数?它是一种可行的解决方案还是更好地使用hasOne关系?
没有每个A对象也是B对象.ES.用户和老师
谢谢
在Django中是否可以使用request.GET的模型方法?例如
class Car(models.Model):
owner = ForeignKey(Owner)
car_model = ...
def car_filter(self, request):
query = request.GET.get("q")
if query:
Car.objects.filter(owner = self.id.order_by('id')
else:
Car.objects.filter(owner = me).order_by('id'
)
Run Code Online (Sandbox Code Playgroud)
?
如果我们的模型定义如下,如何找到图像的高度和宽度
class MModel:
document = FileField()
format_type = CharField()
Run Code Online (Sandbox Code Playgroud)
和图像保存在文档中然后如果它是图像我们如何找到文档的高度和宽度?
models ×10
django ×4
activerecord ×2
filter ×2
python ×2
doctrine ×1
eloquent ×1
filefield ×1
image ×1
key ×1
laravel ×1
laravel-4 ×1
php ×1
rails-models ×1
relationship ×1
ruby ×1
symfony1 ×1
validation ×1