has-and-belongs-to-many

What is the fastest way to create mass HABTM associations in Rails?

霸气de小男生 提交于 2019-12-31 21:43:12
问题 I have two tables, with a HABTM relationship in Rails. Something like the following: class Foo &lt ActiveRecord::Base has_and_belongs_to_many :bars end class Bar &lt ActiveRecord::Base has_and_belongs_to_many :foos end Now I have a new Foo object, and want to mass-assign thousands of bars to it, which I've pre-loaded: @foo = Foo.create @bars = Bar.find_all_by_some_attribute(:a) What's the fastest way to do this? I've tried: @foo.bars = @bars @foo.bars &lt&lt @bars And both run really slow,

Rails: is it possible to add extra attribute to a has_and_belongs_to_many association?

偶尔善良 提交于 2019-12-31 12:18:30
问题 What I mean is if I have two models, connected by a has_and_belongs_to_many association, can I store other data in the join table for each association? That is, the extra data would not be part of a single record in either table, but instead of the connection between them. My actual models are as follows: class Part < ActiveRecord::Base has_and_belongs_to_many :assemblies has_and_belongs_to_many :packages belongs_to :user validates :name, :user_id, :presence => true end class Package <

Symmetrical, self-referential HABTM relationship

蹲街弑〆低调 提交于 2019-12-31 04:05:11
问题 In my schema I have a model called Imprintables. In this model I have this self-referential relationship: class Imprintable < ActiveRecord::Base has_and_belongs_to_many :coordinates, class_name: 'Imprintable', association_foreign_key: 'coordinate_id', join_table: 'coordinates_imprintables' ... end In the imprintable create/edit form I have a select field where users can select coordinate imprintables (coordinates are used to identify imprintables that are similar to the one being created

HABTM relation find all records, excluding some based on association

女生的网名这么多〃 提交于 2019-12-31 03:05:17
问题 I've looked at some of the similar SO posts relating to this but I'm struggling to get my head around it. I have a habtm relation between Projects and Users. I'm trying to find all the Projects that a particular user does not belong to but I don't know how. I've tried this sort of thing: Project.where('project_id != ?', user.id) But it's also obviously wrong. I'm using rails 3.2.x Many of the answers relating to this mention scopes but I haven't come across them before (I'm still very new to

Rails: Find rows without connection in HABTM relation

冷暖自知 提交于 2019-12-30 01:26:27
问题 I have two models, Users and Leads connected with HABTM relation: class Lead < ActiveRecord::Base has_and_belongs_to_many :users end class User < ActiveRecord::Base has_and_belongs_to_many :leads end How can I now get only those Leads which are not connected to Users? Thanks in advance! 回答1: What you're looking for is known as an anti join . There are three standard ways to accomplish this, Using a null left outer join Using a where clause with a sub query with the NOT & IN keywords Using a

Trying to use accepts_nested_attributes_for and has_and_belongs_to_many but the join table is not being populated

亡梦爱人 提交于 2019-12-29 04:20:18
问题 I am learning RoR and trying to use accepts_nested_attributes_for and has_and_belongs_to_many to submit information that would traditionally be two forms. I have read on some sites they are compatible, some sites they aren't compatible, and some sites don't know. As reference, I am using Rails 2.3.4. I tried modeling my solution from the Ryan's Scraps tutorial on nested models From what I have tried to debug, it seems that I have two problems but I am not sure why. When I submit a form with

HasMany, BelongsTo for price comparision problem

大憨熊 提交于 2019-12-25 15:54:12
问题 I want to make a price comparison section on my site. Now I really have trouble understanding the relationships between a Product, a Shop and a Price. A Product is available in multiple Shops, who each have a Price. So I have a table Products, a table Shops and a table Prices, in Prices I store the product_id, the shop_id and the price Just to check, I would define it like this: Product hasManyAndBelongsTo Shop Shop hasManyAndBelongsTo Product Price belongsTo Shop Price belongsTo Product But

CakePHP 3.x - SUM() on ManyToMany

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 07:19:04
问题 I am getting stuck on using SQL functions queries made in CakePHP 3 in combinations with associations. The situation is as follows: I have three tables, a 'products' table, an 'orders' table and a join table called 'orders_products'. In the index of OrdersController I would like to add the total price (= sum of relevant product prices) to the table of orders. In SQL this exactly can be done with the following query: SELECT orders.id, SUM(products.price) FROM orders LEFT JOIN orders_products

Pretty HABTM List Entry

非 Y 不嫁゛ 提交于 2019-12-25 03:50:23
问题 I have a Recipe, Item, and Units table/model. I have a HABTM relationship with Recipe and Item, and I get the default multiple-select box when adding/editing Recipe. (am using Bake for everything for the most part). The problem is I need to associate quantities and units with each Item. Sample of UI I'm hoping for: A big component of it is the ability to add/delete/edit the individual items. I imagine looking at the submitted form data, and using some jquery and clone would work. But I was

Cakephp, Retrieve Data for HABTM Models using conditional find

走远了吗. 提交于 2019-12-24 19:16:20
问题 There are 2 Models: Project & Category that are bind with HABTM relationship. I would like to perform a search from projects controller that can do the following: FIND all DISTINCT Project.scedule WHERE Category.slug != 'uncategorised' Apologies for the syntax, I'm no sequel expert. What I have managed to do is to retrieve all projects that do not belong to Category uncategorised into an array however I'm not sure as to how to search again the array result for DISTINCT Project.schedule values