has-and-belongs-to-many

Rails habtm and finding record with no association

和自甴很熟 提交于 2019-11-28 23:34:03
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 returns Users who belongs to any of given groups (given as an array of id's). scope :in_groups, lambda { |g

CakePHP - problem with HABTM paginate query

别说谁变了你拦得住时间么 提交于 2019-11-28 20:57:45
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` ..... FROM `restaurants` AS `Restaurant` LEFT JOIN `users` AS `User` ON (`Restaurant`.`user_id` =

habtm relationship does not support :dependent option

自闭症网瘾萝莉.ら 提交于 2019-11-28 20:22: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. 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 . If you want to keep to the simple has_and_belongs_to_many association, you could add this: class Person < ActiveRecord::Base has_and_belongs_to_many :posts before_destroy { posts.clear } end Which will clear the join table

has_and_belongs_to_many in Rails

别说谁变了你拦得住时间么 提交于 2019-11-28 19:20:41
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? has_and_belongs_to_many is meant for simple many-to-many relationships. has_many :through, on the other hand, is meant for indirect one-to-many relationships, or many-to-many relationships with

Rails - HABTM Relationship — How Can I Find A Record Based On An Attribute Of The Associated Model

你。 提交于 2019-11-28 18:36:24
I have setup this HABTM relationship in the past and its worked before....Now it isnt and I'm at my wits end trying to figure out whats wrong. I've looking through the rails guides all day and cant seem to figure out what I'm doing wrong, so help would really be appreciated. I have 2 models connected through a join model and I'm trying to find records based an attribute of the associated model. Event.rb has_and_belongs_to_many :interests Interest.rb has_and_belongs_to_many :events and a join table migration that was created like create_table 'events_interests', :id => false do |t| t.column

RoR 3 Creating an Invoice app - How do I create the form for a HABTM invoice/products association?

不想你离开。 提交于 2019-11-28 12:59:37
问题 I'm attempting to make an invoice application. Here are my models which are related to my question: UPDATE: Model information has changed due to recent suggestions Invoice > id > created_at > sales_person_id LineItem > id > invoice_id > item_id > qty_commit (inventory only) > qty_sold > price (because prices change) > ...etc Item > barcode > name > price > ...etc Invoice has_many items, :through => :line_items . Ditto for Item. What I want to do is that when I create a new invoice, I'd like

CakePHP saving data in HABTM fields

北慕城南 提交于 2019-11-28 12:50:41
问题 I am using these three relations User, Movie and UsersWatchlist.. Where users_watchlists contains movie_id and user_id as attributes.. I have this structure array( 'User' => array( 'id' => '3' ), 'UsersWatchlist' => array( 'UsersWatchlist' => array( (int) 0 => '3' ) ) ) public function watchlist($id = null) { $userid = '3'; if (!$id && $userid != 3) { $this->Session->setFlash('Invalid Movie'); $this->redirect($this->referer(array('action' => 'listing'))); } $this->request->data['User']['User'

CakePHP 2.1 saving HABTM fields

和自甴很熟 提交于 2019-11-28 11:40:24
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( 'className' => 'Movie', 'joinTable' => 'users_watchlists', 'foreignKey' => 'user_id', 'associationForeignKey'

How to create has_and_belongs_to_many associations in Factory girl

[亡魂溺海] 提交于 2019-11-28 02:53:16
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 results in an infinite loop as the factories recursively use each other to define themselves. More

Rails: Has and belongs to many (HABTM) — create association without creating other records

给你一囗甜甜゛ 提交于 2019-11-27 21:21:16
Spent all day on Google, but can't find an answer. :\ I have a HABTM relationship between Users and Core_Values. class CoreValue < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many :core_values In my controller, I need to do two separate things: If a CoreValue does not exist, create a new one and associate it with a given user id, and Assuming I know a particular CoreValue does exist already, create the association without creating any new CoreValues or Users For # 1, I've got this to work: User.find(current_user.id).core_values.create({