轨道has_many_through数据插入问题 [英] rails has_many_through data insertion question
问题描述
我有一个场景,模特的样子
I have a scenario where the models look like
create_table :users do |t|
t.string :name
t.timestamps
end
create_table :blogs do |t|
t.string :url
t.string :title
t.text :description
t.timestamps
end
create_table :posts do |t|
t.integer :user_id, :null => false
t.integer :blog_id, :null => false
t.text :post_text
end
class Blog < ActiveRecord::Base
has_many :users, :through =>:posts
has_many :posts, :dependent=>true
end
class User < ActiveRecord::Base
has_many :blogs
has_many :posts, :through=>:blogs
end
class Post < ActiveRecord::Base
belongs_to :blog
belongs_to :user
end
我的问题是: 1.当创建一个用户,我想自动创建一个博客给他。
The question I have is: 1. When a user is created, I would like to create a blog for him automatically.
@user = User.find_or_create_by_name(用户名)
@user = User.find_or_create_by_name(user_name)
我如何去创建一个博客? @blog = Blog.find_or_create_by_user_id(@user)
How do I go about creating a blog? @blog = Blog.find_or_create_by_user_id(@user)
我收到以下错误:
undefined method `find_or_create_by_user_id' for #<Class:0x1044735b0>
@blogs = @user.blogs
给我:
Mysql::Error: Unknown column 'blogs.user_id' in 'where clause': SELECT * FROM `blogs` WHERE (`blogs`.user_id=1234)
我知道博客表没有user_id列。 但事实并非联接应该照顾它? 我在做什么错在这里?
I know Blogs table does not have user_id column. But isn't the join supposed to take care of it? What am I doing wrong here?
感谢您的帮助
推荐答案
要使用Post模型的关联表,用户模式需要进行调整,以正确展示的关联。一旦这样做,你可以使用 after_create
来创建一个新创建的用户一个新的博客。
To use the Post model as the association table, the User model needs to be tweaked to properly demonstrate the association. Once done, you could use after_create
to create a new blog for a newly created user.
class User < ActiveRecord::Base
has_many :posts
has_many :blogs, :through=>:posts
after_create :add_blog
private
def add_blog
blogs << Blog.new
end
end
编辑:
最好的,我知道如何处理这是解释我想的关系,试图实现,那么你告诉我,我要走了,我们从那里。
The best I know how to handle it is to explain what I "think" the relationships are attempting to accomplish then you tell me where I'm off and we go from there.
1),用户可以拥有许多博客
1) A User can "own" many blogs
2)一个博客可以有很多帖子
2) A blog can have many posts
3)职位属于单个用户和单个博客
3) A post belongs to a single user and to a single blog
4)博客中只能有一个老板(用户)
4) a blog can only have one "owner" (user)
5)博客可以拥有,受到了很多用户,从而使他们的权限来发布。
5) Blogs can be "owned" by many users thereby giving them permission to post.
如果1-4是真的,5假的... ...这不是一个的has_many:通过方案,或许多一对多的关系,只是一个一对多的关系。
If 1-4 are true, and 5 false... that isn't a "has_many :through" scenario or many-to-many relationship, just one-to-many relationships.
因此,职位不应被用作关联表。有不需要的关联表
Accordingly, posts should not be used as an association table. There isn't an association table needed.
添加 t.integer:USER_ID,:空=&GT;假
的博客表
class Blog < ActiveRecord::Base
belongs_to :users,
has_many :posts, :dependent=>:destroy # rec'd error in RoR3... replaced true with :destroy
end
class User < ActiveRecord::Base
has_many :blogs, :dependent=>:destroy
has_many :posts
after_create :add_blog
private
def add_blog
blogs << Blog.new
end
end
class Post < ActiveRecord::Base
belongs_to :blog
belongs_to :user
end
如果5是真的,这将是一个真正的多到很多......但我不认为这就是你试图做什么。
If 5 is true, this would be a true many-to-many... but I don't think that's what you're attempting to do.
这篇关于轨道has_many_through数据插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!