false do |t| t"/>
首页 \ 问答 \ 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)

我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)

create_table "tags_pages", :id => false do |t|

  t.integer "tag_id", "page_id"

end

add_index "tags_pages", "tag_id"
add_index "tags_pages", "page_id"

activerecord如何在此表上工作? 我想插入和删除新行。 对不起,如果这是一个noob问题。


create_table "tags_pages", :id => false do |t|

  t.integer "tag_id", "page_id"

end

add_index "tags_pages", "tag_id"
add_index "tags_pages", "page_id"

How activerecord works on this table ? I want to insert and delete new rows. Sorry if it is a noob question.


原文:https://stackoverflow.com/questions/1743231
更新时间:2019-07-13 09:05

最满意答案

假设你有一个页面和一个标签。

# This will add a "tags_pages" entry, linking one page to one tag
page.tags << tag

# This will delete the appropriate "tags_pages" entry
page.tags.delete(tag)

您还可以使用clear方法删除链接到一个页面的所有标签。

page.tags.clear

Let's suppose you have one page and one tag.

# This will add a "tags_pages" entry, linking one page to one tag
page.tags << tag

# This will delete the appropriate "tags_pages" entry
page.tags.delete(tag)

You can also delete all the tags linked to one page with the clear method.

page.tags.clear
2009-11-16

相关问答

更多

从连接表中获取聚合信息(ActiveRecord)(Getting aggregate information from join table (ActiveRecord))

在Sqlite数据库上尝试此查询: Foo.find_by_sql("SELECT foos.*, ss1.ac as awesome_count, SS2.ac as lame_count from foos left outer join (select foo_id, count(*) as ac from bars where awesome = 't' group by foo_id) as SS1 on SS1.foo_id = foos.id left outer join (s ...

在Mysql中使用id从表中删除多行(Delete many rows from a table using id in Mysql)

最好的方法是使用IN语句: DELETE from tablename WHERE id IN (1,2,3,...,254); 如果您有连续的ID,您也可以使用BETWEEN : DELETE from tablename WHERE id BETWEEN 1 AND 254; 您当然可以使用其他WHERE子句限制某些ID: DELETE from tablename WHERE id BETWEEN 1 AND 254 AND id<>10; The best way is to use ...

ActiveRecord:从控制台列表中的列(ActiveRecord: List columns in table from console)

这将从表中列出column_names Model.column_names e.g. User.column_names This will list the column_names from a table Model.column_names e.g. User.column_names

如何在CFSCRIPT中使用EntityDelete从临时表中删除行(How To Delete Rows From Temp Table With EntityDelete In CFSCRIPT)

使用EntityDelete( tempArr[i] ); 在for循环结束时。 tempArr = ORMExecuteQuery( "FROM TEMP WHERE CartID=#FORM.CartID#" ); transaction { for( i=1; i LTE ArrayLen(tempArr); i=i+1 ) { reg = EntityNew( "register" ); reg.setFirstName( tempArr[i].ge ...

删除HTML表中的所有行(Delete all rows in an HTML table)

将<th>行保留在<thead> ,然后在<tbody>的其他行中替换<tbody>一个新的,空的。 即 var new_tbody = document.createElement('tbody'); populate_with_new_rows(new_tbody); old_tbody.parentNode.replaceChild(new_tbody, old_tbody) Keep the <th> row in a <thead> and the other rows in a <t ...

我如何从Gojs中的滚动表中删除一行?(How can I delete a row from a Scrolling Table in Gojs?)

http://gojs.net/latest/samples/selectableFields.html演示了如何做到这一点。 基本上,您支持突出显示各个字段并跟踪突出显示在节点内的字段,从而有效地形成所有突出显示字段的集合。 然后,您可以重写CommandHandler.deleteSelection方法以删除选定的字段(如果有),而不是删除选定的节点。 http://gojs.net/latest/samples/selectableFields.html demonstrates how t ...

在Yii 2.0 Framework中使用ActiveRecord加入查询(Join Query using ActiveRecord in Yii 2.0 Framework)

将公共财产添加到您的优惠券类public $WebsiteName; 作为另一种选择,您可以通过在优惠券模型中定义关系来访问您的WebsiteName : public function getWebsite() { $this->hasOne(Website::classname(), ['websiteid' => 'websiteid']); } 然后更改您的查询: $coupons = Coupon::find()->with('website')->limit(10)->all ...

MySQL:只从一个表中删除“WHERE ... NOT IN”的行(MySQL: delete rows with “WHERE … NOT IN” from only one single table)

改为使用left join : DELETE hrr FROM `home_resident_relations` hrr LEFT JOIN (SELECT tbl.`resident_id` FROM `home_resident_relations` tbl WHERE tbl.`datemodified` = (SELECT max(tbl2.`datemodified`) ...

相关文章

更多

最新问答

更多
  • Unity着色器错误;(Unity shader error; presumably in if statement)
  • 如何在Liferay portlet中设置Cookie?(How to set a Cookie in Liferay portlet?)
  • C#只读访问List <>或其他集合,或ToArray()魔术(C# readonly access to List<> or other collection, or ToArray() magic)
  • 如何使用Google Web工具包创建登录应用程序?(How to create a login application using Google web tool kit? [closed])
  • 如何使我的函数返回结果作为全局变量?(How to make my function return results as a global variable?)
  • 为什么我的网站需要“启用32位应用程序”?(Why does my website need “Enable 32-bit applications”?)
  • 红宝石数组具有相同的值(ruby array of hash with same value)
  • Android(在Scala中):StackOverflowError取决于何时启动线程?(Android (in Scala): StackOverflowError depends on when to start a thread?)
  • 适用于iOS应用的通用链接(Universal Links for iOS apps)
  • 创建并打开文件linux编程(create and open file linux programming)
  • Datepicker,第2个日期是从第1个日期开始的X天(Datepicker, 2nd date is X days from 1st date)
  • 背景大小过渡不起作用(Transition on background-size doesn't work)
  • 在React / Redux中的On Click函数中传递Prop(Passing a Prop in an On Click Function in React/Redux)
  • 关键字在代码中做了什么,是否有没有此关键字的替代方法?(what does the keyword this does in the code and are there any alternate methods without this keyword? [duplicate])
  • 反向设计FoxPro / dBsae数据库以创建EER模型(Reverse Engineer a FoxPro/dBsae database to create the EER model)
  • 在R中的变量中组合具有相同值的行(Combine rows that have same value in a variable in R [duplicate])
  • SQL Server:非空唯一主键(SQL Server: Non-null unique vs. Primary Key)
  • 将扩展ASCII字符代码转换为ISO-8859-1(Convert extended ASCII character codes to ISO-8859-1)
  • 如何在.NET中读取m4a文件中的标签?(How to read tags out of m4a files in .NET?)
  • 呼叫链接的性能提升?(Performance gain on call chaining?)
  • 风景名胜区规划设计文本里面都包含哪些内容?推荐一家旅游规划设计公司?谢谢!
  • 如何在String中将字符串转换为枚举?(How do I convert a string to enum in TypeScript?)
  • SQLite SELECT出现异常如何解决它?(SQLite SELECT gives exception How to fix it?)
  • NSLog不会从ViewController类输出,而是来自AppDelegate类的WILL。(NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class. iOS:Objective C)
  • 更改UIPicker突出显示的标签宽度(change UIPicker highlighted tab width)
  • 空心倒五角形(Hollow inverted pentagon)
  • 如何设置仅使用OpenID的Plone站点(How to setup Plone sites working only with OpenID)
  • Paperclip缺少Amazon S3的协议(https)(Paperclip is missing the Protocol (https) with Amazon S3)
  • 从宏中部署netbeans中的ANT文件(Deploy ANT file in netbeans from macro)
  • XMLReader是未知的(XMLReader is unknown)