0
我正在使用rails标记的博客系统上工作。现在它使用连接表,但我对性能有一些疑问。Rails activeRecord补丁和提交
现在,每次创建博客及其关联标签时,它都会向数据库提交每一条语句。我认为更好的做法应该是修补所有这些语句,并且只向数据库提交一次。在轨道中可能吗?
创建日志转储:
Started POST "/articles" for 127.0.0.1 at 2014-08-29 15:30:48 -0400
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"K4rWQEh0810X2jMe/Z9pC/PN2WeOcO0F0TkjKUZTPec=", "title"=>"test27", "text"=>"ddddd", "tag"=>"a,k,g", "commit"=>"Submit"}
Unpermitted parameters: utf8, authenticity_token, tag, commit
(0.9ms) BEGIN
SQL (0.3ms) INSERT INTO `articles` (`created_at`, `text`, `title`, `updated_at`) VALUES ('2014-08-29 19:30:48', 'ddddd', 'test27', '2014-08-29 19:30:48')
(0.2ms) COMMIT
Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`name` = 'a' LIMIT 1
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `articles_tags` (`article_id`, `tag_id`) VALUES (28, 1)
(0.2ms) COMMIT
Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`name` = 'k' LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `tags` (`name`) VALUES ('k')
(0.1ms) COMMIT
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO `articles_tags` (`article_id`, `tag_id`) VALUES (28, 17)
(8.3ms) COMMIT
Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`name` = 'g' LIMIT 1
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `tags` (`name`) VALUES ('g')
(0.1ms) COMMIT
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO `articles_tags` (`article_id`, `tag_id`) VALUES (28, 18)
(0.2ms) COMMIT
型号设计:
class Article < ActiveRecord::Base
has_and_belongs_to_many :tag
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :article, :uniq => true
end
博客创建代码:
def create
@article = Article.new(article_params)
tag_arr = params[:tag].split(',')
if @article.save
tag_arr.each do |name|
tag = Tag.find_or_create_by(name: name) # create a new tag only if tag.name not exist
@article.tag << tag
end
redirect_to @article
else
render 'new'
end
end