让一个模型同时具有has_many和has_one关系,正确的(Rails)方式是什么?在我的例子中,我希望我的Device模型能够跟踪它的当前位置和所有以前的位置。这是我的尝试,它是有效的,但有没有更好的方法?
模型
class Location < ActiveRecord::Base
belongs_to :device
end
class Device < ActiveRecord::Base
has_many :locations # all previous locations
belongs_to :location # current location
技术: Rails/ActiveRecord 6.1.x 归根结底,可能没有正常的方法来做到这一点,但我的目标是: 通过4个连接将表1连接到表2,最终表1在表2上执行has_many操作。下面是一个示例: Table1 = Person ( has many pets )
Join1 = Pet ( belongs to person, has many pet toys )
Join2 = PetToy ( belongs to pet, belongs to toy maker )
Join3 = ToyMaker ( has many pet toys, belongs to inve
我已经构建了一个ruby on rails应用程序,让用户跟踪他们的锻炼情况。用户has_many锻炼。此外,如果用户是健身房所有者,则可以创建一个盒子(健身房)。其目的是过滤用户的活动,以便他们只能看到与其健身房相关的信息。在这种情况下,锻炼。在一个盒子显示页面上…我想显示所有的用户谁是通过会员资格与该盒子相关联,然后,因此,相关联的用户锻炼。这是设置。
用户
class User < ActiveRecord::Base
has_many :boxes
has_many :workouts, :dependent => :destroy
end
锻炼身体
class W
我在Rails 3中工作,有一个包含多个子表的表,例如
class Foo < ActiveRecord::Base
has_many :things
has_many :items
has_many :widgets
end
class Thing < ActiveRecord::Base
belongs_to :foo
end
class Item < ActiveRecord::Base
belongs_to :foo
end
class Widget < ActiveRecord::Base
belongs_to :foo
end
我有三种型号
a is client
b is team leader
c is worker
现在
a has_many b through ab,
b has_many c through bc和
a has_many c through ac。
ab,bc,ac是由has_many through association创建的连接表
现在
a can create b,
b can create c和
a can create c
也就是说,Client和Team leader都可以创建worker。
现在我需要检索由client和TeamLeader创建的所有workers。虽然我可以
User模型和Period模型之间的表不存在。他们有has_many_and_belongs_to关系。我还在学习Rails,所以请容忍我。
这是User.rb模型文件的内容
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_au
我有一个类似如下的模型设置:
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_many关系,但只能通过该关系中的最新记录,然后可以在该关系上应用筛选器/选择。
下面是一个非常简单的变体,它捕捉了我的奋斗:
假设我有一个Employees表和一个Employments表。一个employee has_many employments。employment有status of :active或:inactive。
class Employee < ActiveRecord::Base
has_many :employments
end
class Employment < ActiveRec
我正在努力让一个关联在Rails中正常工作。我只是在学习这门语言,所以任何帮助都是非常感谢的。
我有:
class Feedback < ActiveRecord::Base
belongs_to :section
end
class Section < ActiveRecord::Base
has_many :questions
has_many :feedbacks
end
我的'feedbacks‘表有一个'section_id’列。
然后在我的/show.html.erb in sections中,我有:
<% @secti
我试图实现两个模型之间的HATBM关联,其中一个模型指向我的本地数据库表,另一个模型指向外部数据库的表。
本地模式:
class Group < ApplicationRecord
has_and_belongs_to_many :chains
end
连接到外部数据库的模型:
class Chain < ApplicationRecord
establish_connection :xyz
self.table_name = 'queues'
has_and_belongs_to_many :groups
end
联接表(Ch
我现在的协会是这样的:
class Location < ApplicationRecord
has_many :instructors
end
class Instructor < AppliactionRecord
belongs_to :location
end
我想介绍一个新的location_instructors表,它连接上面的表。理想情况下,我的协会会变成这样:
class Location < ApplicationRecord
has_many :location_instructors
has_many :instructors, thr
嗨,我正在创建3模型与红宝石rails,但我有一些问题。这是我的模型代码
class Company < ActiveRecord::Base
has_many :pendings
has_many :products, :through => :pendings
end
class Product < ActiveRecord::Base
has_many :pendings
has_many :companies, :through => :pendings
end
class Pending < ActiveRecord::Bas
我有一个连接表,a有多个,属于多个,连接表包含很多其他属性,有时间戳,实现方面没有问题,
用户
class User < ApplicationRecord
has_many :affiliations
has_many :organizations, through: :affiliations
end
组织
class Organization < ApplicationRecord
has_many :affiliations
has_many :users, through: :affiliations
end
从属关系
class Affiliat