简体   繁体   English

自动创建联接表记录,rails 3

[英]Create join table record automatically, rails 3

Right now my Posts model has_many :tags, :through => :tag_joins 现在我的帖子模型has_many:tags,:through =>:tag_joins

When I add tags, while creating a post, the tag_join records are automatically created. 添加标签时,在创建帖子时,将自动创建tag_join记录。

Now here is what I'm trying to accomplish: While viewing the show view of posts I want to be able to add a new tag. 现在,这是我要完成的任务:在查看帖子的显示视图时,我希望能够添加一个新标签。

I tried @post.tag = Tag.new didn't work (returns a "nomethoderror" for tag=) 我尝试了@ post.tag = Tag.new不起作用(返回tag =的“ nomethoderror”)

So I'm trying to figure out how I can add tags and still create those joins automatically. 因此,我试图弄清楚如何添加标签并仍然自动创建这些联接。

I am using accepts_nested_attributes etc. 我正在使用accepts_nested_attributes等。

UPDATE: I originally asked how to do this on the index view, but I have changed it to the show view - because I expect it to be a little easier. 更新:我最初询问如何在索引视图上执行此操作,但是我已将其更改为显示视图-因为我希望它会更容易一些。

You're not too far off with @posts.tags = Tag.new . @posts.tags = Tag.new距离您不太远。 Here's a couple of ways to do it; 这有两种方法可以实现:

@post.tags << Tag.create(params[:tag])
@post.tags.create params[:tag]

I see a couple of approaches to this problem.. One is to pass through the id of the post with the tag form using either a hidden_field or by using nested routes for tags. 我看到了几种解决此问题的方法。一种方法是使用hidden_​​field或使用嵌套的标记路由通过带有标记形式的帖子ID。 Then you can use that in the controller to retrieve the post and use a syntax similar to above. 然后,您可以在控制器中使用它来检索帖子,并使用与上面类似的语法。

While that would work, the problem is that it's a bit ugly.. It means your tag controller would be dealing with finding a post (which isn't necessarily wrong, but it shouldn't need to worry about posts. Unless tags can only be associated with posts, that is). 尽管这行得通,但问题是它有点丑陋。这意味着您的标签控制器将处理找到帖子的问题(这不一定是错误的,但不必担心帖子。除非标签只能与帖子相关联)。

The more graceful way of dealing with it would be to make the form you're showing be a form for the post instance, not a tag. 处理它的更合适的方法是使您显示的表单成为post实例的表单,而不是标签。 Then you could use nested attributes to create the tag as part of a post. 然后,您可以使用嵌套属性在帖子中创建标签。

The key observation here is the difference between .new and .create. 这里的主要观察是.new和.create之间的区别。 For my Devour.space application, I was running into the same issue. 对于我的Devour.space应用程序,我遇到了同样的问题。 If you create the object in memory using: 如果使用以下方法在内存中创建对象:

tag = @post.tags.new(tag_params)
tag.save

There will be no tag_joins entry saved to the database. 将没有tag_joins条目保存到数据库。 @post.tags will not return your new tag. @ post.tags将不会返回您的新标签。 You must use .create at the moment of instantiation or the association will not be recorded in the JOIN table: 您必须在实例化时使用.create,否则关联将不会记录在JOIN表中:

tag = @post.tags.create(tag_params)
@post.tags.last # tag

In my situation, this required a change in how my create action handled requests and errors: 在我的情况下,这需要更改我的create动作处理请求和错误的方式:

has_many :deck_shares
has_many :decks, through: :deck_shares
....

deck = current_user.decks.new(deck_params)
if deck.save # Does not create entry in DeckShares table
  render json: deck
else
  render json: deck.errors, as: :unprocessable_entity
end

This became: 变成:

begin
  deck = current_user.decks.create(deck_params) # creates DeckShare
rescue Exception => e
  render json: e, as: :unprocessable_entity
end
render json: deck unless e

Take a look at the build_xxx or create_xxx methods that the association (belongs_to, has_many etc) add to the models . 看一下关联(belongs_to,has_many等) 添加到模型中的build_xxx或create_xxx方法。 You need to create your tag through the post for rails to 'connect' it automatically. 您需要通过帖子创建标签,以使rails自动“连接”它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM