在我的用户模型中,我可以:
has_many :tasks
Run Code Online (Sandbox Code Playgroud)
在我的任务模型中:
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
然后,假设外键'user_id'存储在tasks表中,我可以使用:
@user.tasks
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何声明has_many关系,以便我可以将用户的任务称为:
@user.jobs
Run Code Online (Sandbox Code Playgroud)
... 要么 ...
@user.foobars
Run Code Online (Sandbox Code Playgroud)
谢谢你的一堆.
这似乎相当简单,但我不能让它出现在Google上.
如果我有:
class City < ActiveRecord::Base
has_many :photos
end
class Photo < ActiveRecord::Base
belongs_to :city
end
Run Code Online (Sandbox Code Playgroud)
我想找到所有没有照片的城市.我很乐意打电话给...
City.where( photos.empty? )
Run Code Online (Sandbox Code Playgroud)
......但那不存在.那么,你如何做这种查询?
更新: 现在找到原始问题的答案,我很好奇,你如何构建逆?
IE:如果我想创建这些作为范围:
scope :without_photos, includes(:photos).where( :photos => {:city_id=>nil} )
scope :with_photos, ???
Run Code Online (Sandbox Code Playgroud) 我正在尝试为与Step具有has_many关系的Recipe模型自定义ActiveAdmin表单.
class Recipe < ActiveRecord::Base
has_many :steps
end
class Step < ActiveRecord::Base
acts_as_list :scope => :recipe
belongs_to :recipe
end
Run Code Online (Sandbox Code Playgroud)
我在与此相关的ActiveAdmin文件中有以下内容:
form do |f|
f.has_many :steps do |ing_f|
ing_f.inputs
end
end
Run Code Online (Sandbox Code Playgroud)
我尝试加载表单时抛出以下错误:
未定义的方法`new_record?' 为零:NilClass
到目前为止,我已经将它与has_many方法隔离开了但是我已经迷失了.任何建议和帮助将不胜感激!
假设我有以下课程
class SolarSystem < ActiveRecord::Base
has_many :planets
end
class Planet < ActiveRecord::Base
scope :life_supporting, where('distance_from_sun > ?', 5).order('diameter ASC')
end
Run Code Online (Sandbox Code Playgroud)
Planet有范围life_supporting和SolarSystem has_many :planets.我想定义我的has_many关系,这样当我询问solar_system所有关联时planets,life_supporting范围会自动应用.基本上,我想solar_system.planets == solar_system.planets.life_supporting.
我不希望改变scope :life_supporting在Planet以
default_scope where('distance_from_sun > ?', 5).order('diameter ASC')
我还想通过不必添加来防止重复 SolarSystem
has_many :planets, :conditions => ['distance_from_sun > ?', 5], :order => 'diameter ASC'
我想要有类似的东西
has_many :planets, :with_scope => :life_supporting
正如@phoet所说,使用ActiveRecord可能无法实现默认范围.但是,我发现了两个潜在的工作.两者都可以防止重复.第一个是长期保持明显的可读性和透明度,第二个是助手类型方法,其输出是显式的.
class SolarSystem < ActiveRecord::Base …Run Code Online (Sandbox Code Playgroud) Ruby和Rails都是新手,但我现在已经预订了教育(显然没什么意义,哈哈).
我有两个模型,Event和User通过表EventUser加入
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through => :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :event
belongs_to :user
#For clarity's sake, EventUser also has a boolean column "active", among others
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
end
Run Code Online (Sandbox Code Playgroud)
这个项目是一个日历,我必须跟踪人们注册并为特定事件划出他们的名字.我认为多对多是一个很好的方法,但我做不到这样的事情:
u = User.find :first
active_events = u.events.find_by_active(true)
Run Code Online (Sandbox Code Playgroud)
因为事件实际上没有那些额外的数据,所以EventUser模型可以.虽然我能做到:
u = User.find :first
active_events = []
u.event_users.find_by_active(true).do |eu|
active_events << eu.event
end
Run Code Online (Sandbox Code Playgroud)
这似乎与"铁路方式"相反.任何人都可以启发我,今晚(今天早上)这已经困扰了我很长时间?
我使用Ember.js与本地存储适配器.更新记录时我遇到了一个奇怪的问题.
我有一个与hasMany关系的帖子和评论模型:
App.Post = DS.Model.extend({
title: DS.attr('string'),
comments: DS.hasMany('comment', {
async: true
})
});
App.Comment = DS.Model.extend({
message: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
这些是我的帖子和评论控制器:
App.PostsController = Ember.ArrayController.extend({
newTitle: '',
actions: {
create: function() {
var title = this.get('newTitle');
var post = this.store.createRecord('post', {
title: title
});
this.set('newTitle', '');
post.save();
}
}
});
App.CommentsController = Ember.ArrayController.extend({
needs: "post",
post: Ember.computed.alias("controllers.post.model"),
newMessage: '',
actions: {
create: function() {
var message = this.get('newMessage');
var comment = this.store.createRecord('comment', {
message: message
});
var post = this.get('post');
var …Run Code Online (Sandbox Code Playgroud) 用户可以将标签添加到代码段:
class Snippet < ActiveRecord::Base
# Relationships
has_many :taggings
has_many :tags, :through => :taggings
belongs_to :closing_reason
end
Run Code Online (Sandbox Code Playgroud)
我想验证标签的数量:至少为1,最多为6.我如何做到这一点?谢谢.
相对较新的rails并尝试使用具有name,gender,father_id和mother_id(2个父项)的单个Person模型来建模一个非常简单的族"树".下面基本上是我想要做的,但显然我不能重复:has_many中的孩子(第一个被覆盖).
class Person < ActiveRecord::Base
belongs_to :father, :class_name => 'Person'
belongs_to :mother, :class_name => 'Person'
has_many :children, :class_name => 'Person', :foreign_key => 'mother_id'
has_many :children, :class_name => 'Person', :foreign_key => 'father_id'
end
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以将has_many与2个外键一起使用,或者根据对象的性别更改外键?或者还有其他/更好的方式吗?
谢谢!
1)我知道autosave: true根据http://railsapi.com/doc/rails-v2.3.8/classes/ActiveRecord/AutosaveAssociation.html保存关联
2)我知道它将保存像这样构造的关联
book = Book.new(name: 'foo')
book.authors.build(name: 'bar') #has_many
book.save
Run Code Online (Sandbox Code Playgroud)
或者喜欢
book = Book.new(name: 'foo')
book.build_author(name: 'bar') #has_one
book.save
Run Code Online (Sandbox Code Playgroud)
3)我认为在分配或添加关联时也会保存关联
book = Book.new(name: 'foo')
book.author = Author.new(name: 'bar')
book.save
Run Code Online (Sandbox Code Playgroud)
要么
book = Book.new(name: 'foo')
book.authors << Author.new(name: 'bar')
book.save
Run Code Online (Sandbox Code Playgroud)
但是,我有一个复杂的错误,涉及的东西不能自动保存,当我期望它.因此,我希望通过检查进行调试,book以验证我认为将要保存的内容实际上将被保存.
TL; DR; 保存关联时检查什么内部状态?我假设一个模型有一个内部实例变量,就像associations_to_save在创建它们时添加了关联.然后,当保存模型时,它会遍历这些关联并保存它们.
有关rails关联的一些新手问题.
我有一个Bug模型和一个状态模型.状态基本上只是一个键/值对表.在可用的选择中,我会说Bug has_one Status最有意义.但是,根据这个
内容belongs_to ContentTemplate.回过头来看看我如何描述问题,你会发现它有效.使用belongs_to,表接受外键的责任.所以Content有一个content_template_id.而且ContentTemplate不需要任何东西.我可以随意指出它.完成.
Bug belongs_to状态更合适(因为Bug应该使用外键).在语义上,他的榜样是有道理的,但我没有.这只是一个轨道的怪癖,在这种情况下,它看起来很奇怪,或者我不理解某事/做错了?
has-many ×10
activerecord ×3
associations ×2
model ×2
ruby ×2
activeadmin ×1
admin ×1
belongs-to ×1
ember-data ×1
ember.js ×1
formtastic ×1
javascript ×1
sql ×1
tagging ×1
validation ×1