has-and-belongs-to-many

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

萝らか妹 提交于 2019-12-03 00:12:11
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 < ActiveRecord::Base has_and_belongs_to_many :parts belongs_to :user end So the point is that each part is

Add record to a has_and_belongs_to_many relationship

一个人想着一个人 提交于 2019-12-02 16:41:19
I have two models, users and promotions. The idea is that a promotion can have many users, and a user can have many promotions. class User < ActiveRecord::Base has_and_belongs_to_many :promotions end class Promotion < ActiveRecord::Base has_and_belongs_to_many :users end I also have a promotions_users table/model, with no id of its own. It references user_id and promotions_id class PromotionsUsers < ActiveRecord::Base end So, how do I add a user to a promotion? I've tried something like this: user = User.find(params[:id]) promotion = Promotion.find(params[:promo_id]) promo = user.promotions

HABTM relation find all records, excluding some based on association

故事扮演 提交于 2019-12-02 01:33:45
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). I just found this post with one answer suggesting: Project.where('id not in (?)', user.projects

Find record, that has ALL associated records

ⅰ亾dé卋堺 提交于 2019-12-01 21:20:34
Say, we have a "Person" and "Favorite" models. "Favorite" is what this person likes: "music", "video", "sport", "internet", "traveling" etc. "Person" HABTM "Favorites", and "Favorite" HABTM "Persons" I need to find a Person, that has ALL listed "Favorites. For example, find a person, that likes "music", "traveling" and "sport". How it can be done, using ActiveRecord.find method ? mark @people = Person.find(:all, :joins => :favourites, :select => "person.*, count(favourites) favourite_count", :conditions => {:favourites => @array_of_favourites}, :group => "persons.id having favourite_count = #{

Rails: HABTM - find all records with no association

心不动则不痛 提交于 2019-12-01 15:02:09
问题 I have 2 models (Workout, Equipment) in a has and belongs to many relationship. If I use Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5") it works, but if I use Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = null") it doesn't return the records with no association. Any ideas? 回答1: Give this a whirl; Workout.joins("left join equipments e on workouts.id = e.workouts_id").where("e.id is null") 来源: https://stackoverflow.com/questions/11279317

CakePHP hasAndBelongsToMany (HABTM) Delete Joining Record

旧街凉风 提交于 2019-12-01 07:17:19
I have a HABTM relationship between Users and Locations. Both Models have the appropriate $hasAndBelongsToMany variable set. When I managing User Locations, I want to delete the association between the User and Location, but not the Location. Clearly this Location could belong to other users. I would expect the following code to delete just the join table record provided the HABTM associations, but it deleted both records. $this->Weather->deleteAll(array('Weather.id' => $this->data['weather_ids'], false); However, I am new to CakePHP, so I am sure I am missing something. I have tried setting

Rails 4 many to many association not working

梦想的初衷 提交于 2019-12-01 06:51:40
Ruby on rails newbie here. Trying to create a starter blog app and having trouble with many to many association between my models. I have 2 models - Post, Category that have a many to many association between each other. My problem: When I create a new post, the Post gets saved but the post-category association does not get saved in the categories_posts table. My code is as below. I appreciate your inputs on this. post.rb class Post < ActiveRecord::Base validates_presence_of :title, :body, :publish_date belongs_to :user has_and_belongs_to_many :categories end category.rb class Category <

Rails 4 many to many association not working

為{幸葍}努か 提交于 2019-12-01 05:16:57
问题 Ruby on rails newbie here. Trying to create a starter blog app and having trouble with many to many association between my models. I have 2 models - Post, Category that have a many to many association between each other. My problem: When I create a new post, the Post gets saved but the post-category association does not get saved in the categories_posts table. My code is as below. I appreciate your inputs on this. post.rb class Post < ActiveRecord::Base validates_presence_of :title, :body,

Has and Belongs to Many error - *_development.hunts_tasks' doesn't exist:

送分小仙女□ 提交于 2019-12-01 01:03:40
So I'm working on a project where there are tasks that make up a scavenger hunt. So when a user clicks on a particular hunt, I'd like the show.html.erb file to show the hunt as well as the tasks associated with that hunt. Both models (Hunt.rb and Task.rb) have "has_and_belongs_to_many" relationships with one another. My controller originally had this code, but it showed every task in the database, instead of just the tasks associated with a particular hunt. def show @hunt = Hunt.find(params[:id]) @title = @hunt.name @tasks = Task.paginate(:page => params[:page]) end So I tried this. def show

Has and Belongs to Many error - *_development.hunts_tasks' doesn't exist:

扶醉桌前 提交于 2019-11-30 19:35:47
问题 So I'm working on a project where there are tasks that make up a scavenger hunt. So when a user clicks on a particular hunt, I'd like the show.html.erb file to show the hunt as well as the tasks associated with that hunt. Both models (Hunt.rb and Task.rb) have "has_and_belongs_to_many" relationships with one another. My controller originally had this code, but it showed every task in the database, instead of just the tasks associated with a particular hunt. def show @hunt = Hunt.find(params[