问题描述
您可以在Rails中 belongs_to的
belongs_to的
关系?
Search results给了我两个结果。我无法找到关于这个问题非常多的信息,这一事实似乎表明,它不应该做或不好的做法。
我问yesterday有关的has_many关系,但认为是因为我找不到这方面的资料,我会产生一个问题,所以很容易为人们对未来搜索此。我转述其他用户的回答(我希望这是确定)。
鞋可以有很多袜子,但只有一个活跃的袜子。其他袜子无效。所有的袜子也具有独特的模式是独一无二的。我要确保我没有袜子相同的模式。我想我可以做到这三种方法
类袜子< ActiveRecord的::基地
belongs_to的:鞋
结束
要找出是否袜子有效或无效的,给予其'主人的鞋,像这样为有效袜子的引用:
类鞋< ActiveRecord的::基地
belongs_to的:袜子
结束
转到它的主人鞋和检查鞋子的积极袜子是当前袜子与否。例如。
高清IS_ACTIVE
owner_shoe.active_sock ==自我
与外键关联它们
类CreateGettingDressed< ActiveRecord的::迁移
高清变化
CREATE_TABLE:鞋子做| T |
t.belongs_to:active_sock,foreign_key:sock_id
t.string:大小
t.timestamps空:假的
结束 CREATE_TABLE:袜子做| T |
t.belongs:owner_shoe,foreign_key:shoe_id
t.string:图案
结束
结束
结束
你的问题是你的功能,两片相互矛盾的:
You're looking to associate the two models on two different associations. Although this is simply done, I feel the way you're trying to do is is a little restricted.
Here's how I'd set up the base association:
#app/models/sock.rb
class Sock < ActiveRecord::Base
#columns id | shoe_id | name | active (boolean) | created_at | updated_at
belongs_to :shoe
end
#app/models/shoe.rb
class Shoe < ActiveRecord::Base
#columns id | name | etc | created_at | updated_at
has_many :socks
scope :active, -> { where(active: true).first }
end
This will give you the ability to call:
@shoe = Shoe.find 1
@shoe.socks.active #-> first sock with "active" boolean as true
It will also negate the need to include an active?
method in your sock
model. You can call @shoe.socks.find(2).active?
to get a response as to whether it's active or not.
Now, this should work pretty well for basic functionality.
However, you mention several extensions:
This adds extra specifications which I'd tackle with a join
model (has_many :through
):
#app/models/sock.rb
class Sock < ActiveRecord::Base
has_many :shoe_socks
has_many :shoes, through: :shoe_socks
end
#app/models/shoe_sock.rb
class ShoeSock < ActiveRecord::Base
# columns id | shoe_id | sock_id | pattern_id | active | created_at | updated_at
belongs_to :shoe
belongs_to :sock
belongs_to :pattern
end
#app/models/shoe.rb
class Shoe < ActiveRecord::Base
has_many :shoe_socks
has_many :socks, through: :shoe_socks, extend: ActiveSock
scope :active, -> { where(active: true).first }
end
You can read more about the below code here:
#app/models/concerns/active_sock.rb
module ActiveSock
#Load
def load
captions.each do |caption|
proxy_association.target << active
end
end
#Private
private
#Captions
def captions
return_array = []
through_collection.each_with_index do |through,i|
associate = through.send(reflection_name)
associate.assign_attributes({active: items[i]})
return_array.concat Array.new(1).fill( associate )
end
return_array
end
#######################
# Variables #
#######################
#Association
def reflection_name
proxy_association.source_reflection.name
end
#Foreign Key
def through_source_key
proxy_association.reflection.source_reflection.foreign_key
end
#Primary Key
def through_primary_key
proxy_association.reflection.through_reflection.active_record_primary_key
end
#Through Name
def through_name
proxy_association.reflection.through_reflection.name
end
#Through
def through_collection
proxy_association.owner.send through_name
end
#Captions
def items
through_collection.map(&:active)
end
#Target
def target_collection
#load_target
proxy_association.target
end
This setup will basically put all the "logic" into the join model. IE you'll have a database of socks, one of shoes and a connecting DB with parings of both.
This will still permit you to call @shoe.socks.active
but without having to degrade the data integrity in your data models.
I have also added some code I wrote a while back - which gives you the ability to access attributes from the join model. It uses the proxy_association
object in ActiveRecord, so it doesn't invoke any more SQL.
This added code will append the active?
attribute to any associative Sock
objects.
这篇关于belongs_to的belongs_to的关联只是没有的has_many或HAS_ONE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!