我的Rails应用程序中有两个类。其中一个是Athlete,另一个是AthleteGameLog。这种关系中有趣的部分是,在多年的过程中,运动员has_many AthleteGameLog的实例。然而,在本赛季的背景下,Athlete每年应该只有一场比赛日志。因此,一个用于2013,一个用于2014,等等。
当使用Ruby on Rails时,has_many关系仍然是正确的方式吗?为了检索我正在查找的所有AthleteGameLog实例的特定年份的正确关系,我是否必须编写scope?
我正试图通过两个层次的has_one关联获得一个activerecord关联,但无法完全解决它。
我有三个模特:
class Dialog < ActiveRecord::Base
belongs_to :contact
end
class Contact < ActiveRecord::Base
has_many :dialogs
belongs_to :location
end
class Location < ActiveRecord::Base
has_many :contacts
end
我想在Dialog模型中创建一个作用域,允许我传入一个位置
我有一个类似如下的模型设置:
class User
has_many :items
has_many :words, :through => :items
end
class Item
belongs_to :user
belongs_to :word
default_scope where(:active => true)
end
class Words
has_many :items
end
我遇到的问题是default_scope没有应用于以下关联:
user.words
而不是这个SQL:
SELECT `words`.* FROM `wor
在Rails中-在has_and_belongs_to_many中使用has_many :through的效果是什么?考虑有两个模型- Posts和Tags,它们具有多对多关系,如下所示:
class Tag < ActiveRecord::Base
has_many :posts_tag
has_and_belongs_to_many :posts
end
class Post < ActiveRecord::Base
has_many :posts_tag
has_many :tags, :through => posts_tag
end
class
我们有三节课
class a
has_many :b
has_many :c, through: b
end
class b
belongs_to :a
belongs_to :c
end
class c
has_many :b
has_many :a, through: :b
validates_uniqueness_of :name
end
当我尝试创建类c的实例时,如果数据库中有两个类a的对象具有相同的name值,我惊讶地发现类c上的验证正在运行!当我根本不应该接触类c行时,为什么还要运行验证呢?
@max要求举个例子。
创建新的Rails 5项目后,请执
我在Rails中找不到任何关于.build方法的文档(我目前使用的是2.0.2)。
通过实验,您似乎可以使用build方法在保存任何一条记录之前将记录添加到has_many关系中。
例如:
class Dog < ActiveRecord::Base
has_many :tags
belongs_to :person
end
class Person < ActiveRecord::Base
has_many :dogs
end
# rails c
d = Dog.new
d.tags.build(:number => "123456")
d.
我在一个Rails3.2应用程序的Project模型中有这样的关联:
has_many :pledges, conditions: { paid: true }
在一个地方,我需要所有未付的誓词。除了两个显而易见的解决方案(使用paid: false条件定义另一个关联或使用Pledge.where...查找承诺)之外,是否可以即时删除这些条件?
类似于:
project.pledges.unscoped.where(paid: false) # does not work since the link to project is also lost
project.pledges.wher
大家好,我是rails的新手,这里有两种制作Drummer模型和Cymbal模型的方法,它们都有很多视频
使用多态的第一种方法:
class Drummer < ActiveRecord::Base
has_many :videos, :as => :videoable
end
class Cymbal < ActiveRecord::Base
has_many :videos, :as => :videoable
end
class Video < ActiveRecord::Base
belongs_to :videoable, :polymo
我有两个模型,Location和Basic,我使用的是和 gem,如何使用地理编码器 near方法向pg_search_scope添加作用域,或者如果有其他方法可以这样做呢?
Location模型具有纬度和经度列。
在我的控制器里,我可以这样做:
# this allows me to search near the Location model
@business = Location.near(params[:loc], 15)
# this searches through the pg_search_scope
@term = Basic.search(params[:q])
#I
我正在做一个小的文本冒险,我想使用ActiveRecord作为对象关系映射。
我遇到麻烦的是,如何用出口把两个房间连接起来。以下是事实:
一个房间可以有多个出口。
出口可以位于不同的方向(它有一个“方向”字段)。此外,它可能还有其他参数,如'locked‘等,我希望稍后添加这些参数。
一个出口连接两个房间。
然而,现在我被困住了:
到目前为止我拥有的
class Room < ActiveRecord::Base
has_many :exits
has_many :neighbours, through: :exits
end
class E
在rails作用域中可以同时使用distinct和where吗?
# this works
has_many: specialties, -> { distinct }, through: :doctor_specialties
# this also works
has_many: specialties, -> { where "name != 'Jeff'" }, through: :doctor_specialties
是否可以将它们合并到一个作用域中?
在使用:uniq => true和has_many :through时,Rails4引入了一个弃用警告。例如:
has_many :donors, :through => :donations, :uniq => true
生成以下警告:
DEPRECATION WARNING: The following options in your Goal.has_many :donors declaration are deprecated: :uniq. Please use a scope block instead. For example, the following:
我有一个rails 5项目,我从一月份开始就在做这个项目,今天我注意到我可以在不指定父记录的情况下创建子记录。这相当令人担忧,因为我验证了我使用的是rails 5.1.2和ruby 2.4.1。该网站仍在开发中。我打开了rails控制台,并测试了我的一些其他模型,它们应该有父验证(在rails 5中应该是默认的验证),这些记录也是在没有关联的情况下创建的。
这没有任何意义。我确实知道这在某种程度上是有效的。最大的变化发生在本周早些时候,我在gem中添加了用于文件上传的“回形针”。对于要检查的内容,有人有什么建议或想法吗?
class Order < ApplicationRecord
我正在为论坛/聊天室构建一个带有Rails的API。我有三个模特:
class User < Application record
has_many messages
has_many :forums, through: :messages
class Message < ApplicationRecord
belongs_to :user
belongs_to :forum
end
class Forum < ApplicationRecord
has_many :messages
has_many :users, through: :messa
我有两个模型,一个叫User,另一个叫Recruiter。我想要做的是能够创建一个搜索用户并返回结果的范围,这样招聘人员就可以看到他们。但我不确定如何建立协会。我在用户和招聘人员之间建立了直通关联,并创建了一个名为recruiter_users的新连接表,但我不确定这是否是正确的方法。
1)在两个模型之间建立关联的最佳方式是什么? 2)如何准确地在招聘人员视图中显示用户结果?
class RecruiterUser < ApplicationRecord
# this is a join model between recruiters and users
belongs_
当我在Rails应用程序上运行一个rspec测试文件时,除了对has_many关联的测试之外,一切都通过了:
it { should have_many(:notes) } # ERROR
it { should belong_to(:agent_field_set) } # OK
it { should have_and_belong_to_many(:profiles) } # OK
should have_many(:notes)行将引发ArgumentError: wrong number of arguments (0 for 1)。我检查了我的数据库和模型文件以验证has_many
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
irb中的:
> u = User.first
=> #<xxxxx>
> u.posts.class
=> Array
My env:
$ruby -v
ruby 1.9.2p312
$rails --version
--version
Rails 3.1.0
我记得几乎所有的关系查询都返回rails3中的ActiveRe
假设我只想在rails.but上的ruby中为用户制作一个表,我想根据用户的角色再制作两个模型。这样我就可以在它们之间使用另一个多对多关系。 class Patient < User
has_many :appointments
has_many :physicians, through: :appointments
end class Physician < User
has_many :appointments
has_many :physicians, through: :appointments
end class User < Applicatio