Rails: belongs_to multiple > Methods

瘦欲@ 提交于 2019-12-23 03:56:04

问题


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.

So:

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

and

has_many :children, through: :family

But that didn't work.

When I try to use:

User.family.new

or

User.child.new 

...it says the method doesn't exist. I take that to mean that it isn't understanding the relationships.

What am I doing wrong?

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

回答1:


You don't have these methods:

User.family.new
User.child.new

When you define has_many association, you just have these methods to create new object associated:

collection.build(attributes = {}, …)
collection.create(attributes = {})

Follow Rails guide:

collection is replaced with the symbol passed as the first argument to has_many

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

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.


来源:https://stackoverflow.com/questions/13614063/rails-belongs-to-multiple-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!