gpt4 book ai didi

mysql - Rails 中的多对多关联

转载 作者:行者123 更新时间:2023-11-29 06:15:30 25 4
gpt4 key购买 nike

这对我来说是一场噩梦,我愿意向能够帮助我的人伸出援手和眼球:P我将尽可能最好地解释这一点。

当用户编辑他们的“个人资料”时,他们可以在嵌套属性字段中输入他们拥有的技能(他们可以单击“添加其他技能”,它将填充另一个字段供他们输入)。当他们开始输入时,jquery-autocomplete 就会启动。他们必须从自动完成中选择一个项目。例如,当他们保存 3 个技能时,我们会得到:

表格:技能

+-----------+----------------+
| user_id | skill |
+-----------+----------------+
| 4 | Photoshop |
+-----------+----------------+
| 4 | Illustrator |
+-----------+----------------+
| 4 | InDesign |
+-----------+----------------+

现在,我想做的不是存储实际的技能名称。相反,我想存储 skills_vocabulary 列表中的技能 ID(这是 jquery-autocomplete 使用的)。

表:skills_vocabulary

+------+----------------+
| id | skill |
+------+----------------+
| 1 | Illustrator |
+------+----------------+
| 2 | Dreamweaver |
+------+----------------+
| 3 | After Effects |
+------+----------------+
| 4 | InDesign |
+------+----------------+
| 5 | Photoshop |
+------+----------------+
| 6 | Flash |
+------+----------------+

因此,技能表应如下所示:

+-----------+----------------+
| user_id | skill_id |
+-----------+----------------+
| 4 | 5 |
+-----------+----------------+
| 4 | 1 |
+-----------+----------------+
| 4 | 4 |
+-----------+----------------+

任何帮助将非常非常非常感谢。我已经为此奋斗了令人尴尬的三个月。

谢谢大家。

编辑/

<%= f.fields_for :skills do |builder| %>
<%= render "skills_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add a Skill", f, :skills %></p>

_skills_fields.html.erb

<div class="fields ac skills">
<div class="clearfix">
<span class="left"><%=f.autocomplete_field :name, users_autocomplete_skills_vocab_name_path, :class => 'restricted_to_autocomplete' %></span><span class="remove_link left"><%= link_to_remove_fields "[x]", f %></span>
</div>
</div>

编辑2/

现在只是

技能.rb

class Skill < ActiveRecord::Base
belongs_to :user
end

用户.rb

class User < ActiveRecord::Base
has_many :tskills, :dependent => :destroy
accepts_nested_attributes_for :tskills, :allow_destroy => true, :reject_if => lambda { |a| a[:name].blank? }
end

skills_vocabulary.rb

class SkillsVocabulary < ActiveRecord::Base

end

我的 Controller 代码是基本索引、显示、新建、创建、更新、销毁

技能列表(大约 10,000 项长)位于表 skills_vocabularies

最佳答案

这应该是你的类(class):

class User < ActiveRecord::Base
has_and_belongs_to_many :skills
end

class Skill < ActiveRecord::Base
has_and_belongs_to_many :users
end

这就是创建它们的迁移

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.timestamps
end
end

def self.down
drop_table :users
end
end

class CreateSkills < ActiveRecord::Migration
def self.up
create_table :skills do |t|
t.string :name
t.timestamps
end
end

def self.down
drop_table :skills
end
end

class SkillsUsers < ActiveRecord::Migration
def self.up
create_table :skills_users, :id => false do |t|
t.integer :skill_id
t.integer :user_id
end
end

def self.down
drop_table :skills_users
end
end

然后添加新技能就这么简单

user.skills << Skill.find(1) # to add a skill
user.skills = Skill.all # to add many of them

here了解更多详情

更新:我已经尝试过自动完成 gem ,我认为你应该接受 Rubish Gupta 建议,即使用我的模型加上 jquery token input因为它可以很好地处理多对多关联。据我所知,自动完成 gem 用于 1 对 n 关联。

此外,您还有一个full working example在 Rails 中玩。

关于mysql - Rails 中的多对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6845675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com