has-and-belongs-to-many

Rails 4 has_and_belongs_to_many

不问归期 提交于 2019-12-08 04:32:46
问题 I have two models. Venues and Categories. They are related to each other as has_and_belongs_to_many Categories are pre populated and in the form i want to display a multi-select to allow choosing the categories for a venue while adding a venue. venue.rb class Venue < ActiveRecord::Base has_and_belongs_to_many :categories end category.rb class Category < ActiveRecord::Base has_and_belongs_to_many :venues end Join Table create_table "categories_venues", id: false, force: true do |t| t.integer

Paginate results filtered by condition on associated model (HABTM) using Containable

橙三吉。 提交于 2019-12-08 03:04:07
问题 I need to paginate list of Product s belonging to specific Category (HABTM association). In my Product model I have var $actsAs = array('Containable'); var $hasAndBelongsToMany = array( 'Category' => array( 'joinTable' => 'products_categories' ) ); And in ProductsController $this->paginate = array( 'limit' => 20, 'order' => array('Product.name' => 'ASC'), 'contain' => array( 'Category' => array( 'conditions' => array( 'Category.id' => 3 ) ) ) ); $this->set('products', $this->paginate());

CakePHP 3: CounterCache with BelongsToMany

感情迁移 提交于 2019-12-08 02:35:56
问题 I have a BelongsToMany association, my tables are PostsTable , TagsTable and PostsTagsTable . As explained here in the CakePHP book (associations), I have this fields: tags.id, tags.tag, tags.post_count posts_tags.id, posts_tags.tag_id, posts_tags.post_id Everything works correctly for now. So, as you can understand, now I want to use the tags.post_count field with CounterCache . I followed the CakePHP book, but I suppose this is a special case, in fact it doesn't work by simply adding the

Rails RESTful delete in nested resources

空扰寡人 提交于 2019-12-07 19:36:41
问题 Okay, so here's an example scenario. There is a student resource resources :students , and students has and belongs to many collections: resources :clubs , resources :majors , etc. So we can set up our routes easily enough... resources :clubs do resources :students end resources :majors do resources :students end resources :students do resources :clubs resources :majors end which generates us a bunch of standard RESTful routes /clubs /clubs/:id /clubs/:club_id/students /clubs/:club_id

How can I update hasandbelongstomany relations for multiple models at once in strongloop loopback

断了今生、忘了曾经 提交于 2019-12-07 15:22:22
问题 I have 2 models in a Strongloop Loopback API Products Tags Between those 2 models I have defined a hasAndBelongsToMany-relation . In my CMS I want a bulk-update functionality for my products, in which I can select many Products, and assign many tags, in one action. How can I save those easily to my Mysql-DB, without having to iterate over each product, then iterate over each tag, and link those 2 ? I checked in the docs and found the add and remove functions, but those only connect one model

Deleting HABTM Association Record

只谈情不闲聊 提交于 2019-12-07 12:41:51
问题 I have two models, Posts and Tags. Posts HasAndBelongsToMany Tags. Let's say a Post 1 has the tags world, news, and paper. Now in the joining table, I want to remove the association between the Tag "paper" and Post 1, but the Tag "paper" should not be deleted from the tags table. Only the association in the joining table should be deleted. How do I do this in CakePHP? 回答1: When you do any HABTM operation other than adding a new one, Cake deletes and recreates the associated join table rows.

Rails HABTM after_add callback fires before saving primary object

筅森魡賤 提交于 2019-12-07 09:08:22
问题 I have two ActiveRecord models having a HABTM relationship with eachother. When I add an AccessUnit through a form that allows zones to be added by checking checkboxes I get an exception that the AccessUnitUpdaterJob can't be enqueued because the access unit passed can't be serialized (due to the fact that the identifier is missing). When manually calling save on the primary object, the issue is resolved but of course this is a workaround and not a proper fix. TLDR; it seems the after_add

Validating length of habtm association without saving

孤街醉人 提交于 2019-12-07 05:46:04
问题 I have a user model with a HABTM relationship to groups. I do not want a user to be able to be in more than 5 groups, so would like to validate the length of the HABTM relationship. On the edit user page I have a list of checkboxes where the user can select the groups they want to be in (I'm using formtastic for the form). In my users controller I'm calling: @user.update_attributes(params[:user]) which is causing rails to update the associations automatically. In my user model I have the

Has and belongs to many relationship with multiple databases

我的梦境 提交于 2019-12-07 01:04:16
问题 I have a situation where I have two models, companies and permissions, where companies is in a separate database from my permissions database. This is a has and belongs to many relationship because each company can have many permissions and each permission can belong to many companies. The reason the two databases are split is because the company database runs a high demand production application and the permissions database controls the permissions for another application. With rails, it

Efficient ActiveRecord has_and_belongs_to_many query

夙愿已清 提交于 2019-12-06 21:44:28
问题 I have Page and Paragraph models with a has_and_belongs_to_many relation. Given a paragraph_id, I'd like to get all matching pages. e.g.: pages = Paragraph.find(paragraph_id).pages.all However, this takes two queries. It can be done in one query: SELECT "pages".* FROM "pages" INNER JOIN "pages_paragraphs" ON "pages_paragraphs"."page_id" = "pages"."id" WHERE "pages_paragraphs"."paragraph_id" = 123 But can this be done without using find_by_sql without modifications to the page_paragraphs table