has-and-belongs-to-many

Rails, Ransack: How to search HABTM relationship for “all” matches instead of “any”

给你一囗甜甜゛ 提交于 2019-11-27 18:28:30
问题 I'm wondering if anyone has experience using Ransack with HABTM relationships. My app has photos which have a habtm relationship with terms (terms are like tags). Here's a simplified explanation of what I'm experiencing: I have two photos: Photo 1 and Photo 2. They have the following terms: Photo 1: A, B, C Photo 2: A, B, D I built a ransack form, and I make checkboxes in the search form for all the terms, like so: - terms.each do |t| = check_box_tag 'q[terms_id_in][]', t.id If I use: q[terms

Rails has_and_belongs_to_many migration

こ雲淡風輕ζ 提交于 2019-11-27 16:47:05
I have two models restaurant and user that I want to perform a has_and_belongs_to_many relationship. I have already gone into the model files and added the has_and_belongs_to_many :restaurants and has_and_belongs_to_many :users I assume at this point I should be able to do something like with Rails 3: rails generate migration .... but everything I have tried seems to fail. I'm sure this is something really simple I'm new to rails so I'm still learning. You need to add a separate join table with only a restaurant_id and user_id (no primary key), in alphabetical order . First run your migrations

Conditions to paginate for belongsToMany CakePHP 3

放肆的年华 提交于 2019-11-27 16:45:08
问题 I have the tables Semesters, Disciplines and a jointTable Semesters_Disciplines. I want to create a action index in DisciplinesController with a semester_id as parameter, which list with paginate just the disciplines what belongs to the semester with the id passed in the parameter. I tried this: public function index($semester_id) { $options = ['semester_id' => $semester_id]; $this->paginate = ['conditions' => $options]; $this->set('disciplines', $this->paginate($this->Disciplines)); $this-

Rails habtm and finding record with no association

故事扮演 提交于 2019-11-27 15:05:27
问题 I have 2 models: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end I want to make a scope (that's important - for efficiency and for ability to chain scopes) that returns Users that doesn't belong to ANY Groups. After many tries, I failed in doing a method instead of scope, which makes collect on User.all which is ugly and.. not right. Any help? And maybe for 2nd question: I managed to make a scope that

CakePHP - problem with HABTM paginate query

家住魔仙堡 提交于 2019-11-27 13:20:30
问题 Tables restaurants cuisines cuisines_restaurants Both restaurant and cuisine model are set up to HABTM each other. I'm trying to get a paginated list of restaurants where Cuisine.name = 'italian' (example), but keep getting this error: 1054: Unknown column 'Cuisine.name' in 'where clause' Actual query it's building: SELECT `Restaurant`.`id`, `Restaurant`.`type` ..... `Restaurant`.`modified`, `Restaurant`.`user_id`, `User`.`display_name`, `User`.`username`, `User`.`id`, `City`.`id`,`City`.`lat

habtm relationship does not support :dependent option

谁说我不能喝 提交于 2019-11-27 12:54:48
问题 Is it true that HABTM relationships do not support the :dependent option? class Person < ActiveRecord::Base has_and_belongs_to_many :posts, :dependent => :destroy end I am trying rails edge. 回答1: Yep, It doesn't support it. See the docs. Generally habtm is meant only for very very simple cases and if you start needing more complex things you should switch to has_many :through . 回答2: If you want to keep to the simple has_and_belongs_to_many association, you could add this: class Person <

has_and_belongs_to_many in Rails

喜夏-厌秋 提交于 2019-11-27 12:13:24
问题 Is there anything explicitly wrong with using has_and_belongs_to_many associations in rails instead of has_many :through? I'm aware of these articles describing differences and work arounds, but they are from 2006. From things I've read on SO, it seems like people think that habtm is old and clunky, but what if a simple many to many join with no model necessary is what you're looking for? Thoughts? 回答1: has_and_belongs_to_many is meant for simple many-to-many relationships. has_many :through,

CakePHP 2.1 saving HABTM fields

折月煮酒 提交于 2019-11-27 06:25:16
问题 I have two models User and Movie.. Where those are associated with UsersWatchlist.. public $hasAndBelongsToMany = array('User' => array( 'className' => 'User', 'joinTable' => 'users_watchlists', 'foreignKey' => 'movie_id', 'associationForeignKey' => 'user_id', 'unique' => 'keepExisting', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' )) public $hasAndBelongsToMany = array( 'Movie' => array(

Rails migration for has_and_belongs_to_many join table

£可爱£侵袭症+ 提交于 2019-11-27 02:36:39
How do I do a script/generate migration to create a join table for a has_and_belongs_to_many relationship? The application runs on Rails 2.3.2, but I also have Rails 3.0.3 installed. Where: class Teacher < ActiveRecord::Base has_and_belongs_to_many :students end and class Student < ActiveRecord::Base has_and_belongs_to_many :teachers end for rails 4: rails generate migration CreateJoinTableStudentTeacher student teacher for rails 3: rails generate migration students_teachers student_id:integer teacher_id:integer for rails < 3 script/generate migration students_teachers student_id:integer

How to create has_and_belongs_to_many associations in Factory girl

故事扮演 提交于 2019-11-26 23:56:22
问题 Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :users end how do you define factories for companies and users including the bidirectional association? Here's my attempt Factory.define :company do |f| f.users{ |users| [users.association :company]} end Factory.define :user do |f| f.companies{ |companies| [companies.association :user]} end now I try Factory :user Perhaps unsurprisingly this