Rails:belongs_to多个>方法 [英] Rails: belongs_to multiple > Methods
问题描述
我刚刚完成了Michael Hartl Rails教程.试图做我自己的东西.
I just finished the Michael Hartl Rails Tutorial. Trying to make something of my own.
我很难理解自己想要建立的关系.我将尽力简化此过程.一棵有树枝,树枝和树叶的树是合适的,但是...
I am having trouble with getting rails to understand the relationships I'm trying to create. I'll try to simplify this as much as possible. A tree with branches, twigs and leaves would be apt, but...
我将以父子格式为例.因此,我自然有用户,可以说用户创建了家庭.
I'll use the parent child format as an example. So naturally I have users, and let's say users create families.
所以:
Class User < ActiveRecord::Base
has_many :families
has_many :parents
has_many :children
end
Class Family < ActiveRecord::Base
belongs_to :user
has_many :parents
end
Class Parent < ActiveRecord::Base
belongs_to: :user
belongs_to: :family
has_many: :children
end
Class Child < ActiveRecord::Base
belongs_to :user
belongs_to :parent
end
如您所见,我希望孩子属于属于家庭的父母,但是孩子本身不属于家庭,除非通过父母.如果这不是一个隐喻,那是可悲的,但在这种特殊情况下是正确的. ;)
As you can see, I want a child to belong to the parent which belongs to the family, but the child itself does not belong to a family except through the parent. Sad if this weren't a metaphor, but true in this particular case. ;)
我已经在父母"下尝试过
I have tried under Parent:
has_many :children, through: :parent
和
has_many :children, through: :family
但这没用.
当我尝试使用时:
User.family.new
或
User.child.new
...它说该方法不存在.我的意思是说它不了解这些关系.
...it says the method doesn't exist. I take that to mean that it isn't understanding the relationships.
我在做什么错了?
如果相关的话,目前在家庭,父母,孩子表中唯一的是这些列:
If it's relevant, the only thing in the family, parent, child tables for now are these columns:
t.string :content
t.integer :user_id
推荐答案
您没有以下方法:
User.family.new
User.child.new
定义has_many
关联时,只需使用以下方法来创建新的关联对象:
When you define has_many
association, you just have these methods to create new object associated:
collection.build(attributes = {}, …)
collection.create(attributes = {})
遵循Rails指南:
集合被作为第一个参数传递的符号代替 has_many
collection is replaced with the symbol passed as the first argument to has_many
您可以查看 has_many关联参考以获得更多信息.
因此,如果User
要创建新的家庭或孩子,则需要使用以下方法:
You can look at this has_many association reference for more info.
So, If User
want create new family or child, you need to use these methods:
user = User.create(name: "ABC") # create new user and save to database.
user.families.build # create new family belongs to user
user.children.build # create new children belongs to user
user.families.create # create new family belongs to user and save it to database.
user.children.create # create new child belongs to user and save it to database.
如果您想让孩子属于属于家庭的父母,则可以修改您的关联:
If you want to get children belongs to parent which belongs to family, you can modify your association:
Class Family < ActiveRecord::Base
belongs_to :user
has_many :parents
has_many :children, through: :parents # Get children through parents
end
Class Parent < ActiveRecord::Base
belongs_to: :user
belongs_to: :family
has_many: :children
end
Class Child < ActiveRecord::Base
belongs_to :user
belongs_to :parent
end
现在,您可以使用以下方法获得所有属于一个家庭的父母的孩子(我支持家庭的ID = 1):
Now, you can get all children belongs to parents which belongs to a family ( I suppost family have id = 1) with:
f = Family.find(1) # Find a family with id = 1
f.children # Get all children in family.
这篇关于Rails:belongs_to多个>方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!