首页 \ 问答 \ 基于嵌套对象存在修改has-many中的关联(Modifing association in has-many through based on nested object existance)

基于嵌套对象存在修改has-many中的关联(Modifing association in has-many through based on nested object existance)

我一直在努力解决问题的最佳方法。 我有一个很多的模型,其中列表包含许多单词,单词可以在许多列表中。 关联表是Lists_word。

我正在使用accepts_nested_attributes_for:Words

单词通过列表控制器作为嵌套属性提交。

我想要实现的逻辑是:

  • 如果用户修改了单词,我不希望在引用该单词的所有其他列表中更改该单词。
  • 如果用户修改单词并且它是新单词,则创建单词并将其与用户列表相关联。
  • 如果用户修改了一个单词并且它存在,则更改关联。
  • 如果用户添加了单词,并且单词已经存在,则只需添加关联。
  • 如果用户添加单词并且不存在则将该单词添加到集合中。

为了实现这一点,我在Lists模型中编写了一个create_or_associate模块。 这是有效的,但我有一种强烈的感觉,有一种更好的方法。

def create_or_associate
# For each word submitted
self.words.each do |the_list|
  if the_list.word_changed? #(New and Changed)

    if the_list.id.nil? #new list word
      if Word.exists?(word: the_list.word)
        self.words << Word.where(word: the_list.word)
      else
        #new list word not in DB
        self.words << the_list
      end
    else
      # a changed list word
      if Word.exists?(word: the_list.word)
        # changed word already in DB
        self.words << Word.where(word: the_list.word)
        self.words.find(the_list.id).delete
      else
        #changed word not in DB
        new_word=Word.create(word: the_list.word)
        self.words << new_word
        self.words.find(the_list.id).delete
      end
    end
  end
end
end

在我的旅行中,我没有看到任何类似的代码,这些代码响起警钟,也许我不在正确的轨道上。

谢谢你的帮助


I've been struggling with the best way to tackle an issue for awhile now. I have a has-many through model where Lists contain many Words, Words can be in many lists. The association table is Lists_word.

I'm using a accepts_nested_attributes_for: Words

the words are submitted via the lists controller as nested attributes.

The logic I wanted to implement however is:

  • If a user modifies a word I don't want that word to change in all other lists referencing that word.
  • If a user modifies a word and it's a new word, then create a word and associate it to the users list.
  • if a user modifies a word and it exists then change the association.
  • if a user adds a word, and word already exists then just add the association.
  • if a user adds a word and it doesn’t exist add the word to the collection.

To accomplish this I wrote a create_or_associate module in the Lists model. This works, however I have a strong feeling that there is a better way.

def create_or_associate
# For each word submitted
self.words.each do |the_list|
  if the_list.word_changed? #(New and Changed)

    if the_list.id.nil? #new list word
      if Word.exists?(word: the_list.word)
        self.words << Word.where(word: the_list.word)
      else
        #new list word not in DB
        self.words << the_list
      end
    else
      # a changed list word
      if Word.exists?(word: the_list.word)
        # changed word already in DB
        self.words << Word.where(word: the_list.word)
        self.words.find(the_list.id).delete
      else
        #changed word not in DB
        new_word=Word.create(word: the_list.word)
        self.words << new_word
        self.words.find(the_list.id).delete
      end
    end
  end
end
end

I've not seen any similar code in my travels, which rings alarm bells that perhaps I'm not on the right track.

Thanks for any help


原文:https://stackoverflow.com/questions/20592126
更新时间:2022-04-14 21:04

最满意答案

问题不明确,但据我所知,你在t2 - > t1中有多对一关系,并且你希望在t2上插入多行时,应在t1中插入一行。 t2上的put语句级别触发器的解决方案,您需要为每个行关键字删除; 如果此记录在t1中可用,您还需要应用内部检入触发器。


Hello sorry for the late answer and the unclear question. I solved the question myself. I created a staging table with a trigger and this works fine for me.

Thank you all for your input.

相关问答

更多
  • 你是否在同一时间获得所有这些错误,或者在尝试不同的事情时遇到不同的错误? 如果您在NEW.product_id之前省略: ORA-00904(可能与之关联的ORA-00933),PLS-00801可能来自两者之间的空间 (即: NEW.product_id 。不知道如何在同一时间获得两者。 因为它现在发布它看起来很好 - 你仍然得到消息Errors: check compiler log TRIGGER T1 compiled后Errors: check compiler log - 或者你在SQL Dev ...
  • 尝试这个, CREATE or REPLACE TRIGGER test AFTER INSERT OR UPDATE OR DELETE ON tabletest REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW DECLARE << Your declarations>> BEGIN IF INSERTING THEN <> END IF; IF UPDATING THEN <
  • 不,不可能在触发器中将两个表放入ON子句中。 但是你可以创建两个触发器,每个表都有一个触发器。 您可以使用INSTEAD OF触发器创建可更新视图 ,如@Littlefoot所示。 No, it's not possible to put two tables in the ON clause in a trigger. But you can create two triggers, one for each table. You can also create an updatable view wit ...
  • 问题不明确,但据我所知,你在t2 - > t1中有多对一关系,并且你希望在t2上插入多行时,应在t1中插入一行。 t2上的put语句级别触发器的解决方案,您需要为每个行关键字删除; 如果此记录在t1中可用,您还需要应用内部检入触发器。 Hello sorry for the late answer and the unclear question. I solved the question myself. I created a staging table with a trigger and this ...
  • 好吧,由于信息太少,我无法猜测你需要什么extamante。 但是根据一个建议: create table TABLE1( "ID1" number not null , "A1" varchar(20) , "B1" varchar(20) , "C1" varchar(20) , "D1" varchar(20) ) / create or replace trigger TG_BIU_TABLE1 after insert or update on TABLE1 for e ...
  • 试试这样, CREATE OR REPLACE TRIGGER VerifNbrHeureMat BEFORE INSERT OR UPDATE ON MATIERES FOR EACH ROW DECLARE l_hour modules.nbr_heure%TYPE; BEGIN SELECT nbr_heure INTO l_hour FROM modules WHERE id_module = :NEW.ID_MODULE; ...
  • 首先,我建议至少对salhistory表进行两次更改: 将saldif定义为计算列 添加列以在发生更改时存储日期时间,否则您将无法告知另一个更改。 这就是说salhistory的表salhistory可能看起来像 CREATE TABLE salhistory ( empno NUMBER NOT NULL, ename VARCHAR2(32) NOT NULL, oldsal NUMBER(10,2) NOT NULL, newsal NUMBER(10,2) NOT NULL, ...
  • 我现在无法测试,但我会尝试类似的东西 create or replace trigger secondary_tasks_bi before insert or update on secondary_tasks for each row declare v_dummy varchar2(1); begin select null into v_dummy from tasks where code = :new.code_primary and end_ ...
  • 您可以在B上添加触发器,并在此触发器中对具有引用值的所有行执行A更新: create trigger b_trg after update on B for each row begin update a set a.b_id = a.b_id where a.b_id = :old.b_id ; end; / you can add a trigger on B and within this trigger perform an update on A for a ...
  • 编辑了有一个FOREIGN KEY防止孤立CLIENT_ID的信息。 要生成一个强制执行您所描述的规则的触发器,以下内容将起作用。 首先,创建一些测试表: CREATE TABLE INSERT_TABLE( CLIENT_ID NUMBER NOT NULL PRIMARY KEY, INSERT_DATE DATE NOT NULL ); CREATE TABLE EXIT_TABLE( CLIENT_ID NUMBER NOT NULL REFERENCES INSERT_TABLE(C ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。