has-and-belongs-to-many

How do I remove a single HABTM associated item without deleting the item itself?

孤人 提交于 2019-11-30 12:38:08
问题 How do you remove a HABTM associated item without deleting the item itself? For example, say I have 3 Students that are in a Science class together. How do I remove the Science objects from the StudentsClasses table without deleting the actual Science reference? I'm guessing that Student.Classes.first.delete isn't a good idea. I'm using JavaScript with drag-and-drop for adding and removing, not check boxes. Any thoughts? 回答1: I tend to use has_many :through, but have you tried student.classes

Multiple select issue with a HABTM relationship using Rails 4

我只是一个虾纸丫 提交于 2019-11-30 11:54:48
问题 Although the code seems to be right, when I try to send the form, the values of the multiple select aren't being sent. If I just remove the multiple option, everything works as expected considering just one value, but it's important to store more than one tag per transaction. Model Transaction.rb class Transaction < ActiveRecord::Base has_and_belongs_to_many :tags Tag.rb class tag < ActiveRecord::Base has_and_belongs_to_many :transactions View <%= form.collection_select :tag_ids, @tags, :id,

HABTM - uniqueness constraint

荒凉一梦 提交于 2019-11-30 11:14:28
I have two models with a HABTM relationship - User and Role. user - has_and_belongs_to_many :roles role - belongs_to :user I want to add a uniqueness constraint in the join (users_roles table) that says the user_id and role_id must be unique. In Rails, would look like: validates_uniqueness_of :user, :scope => [:role] Of course, in Rails, we don't usually have a model to represent the join relationship in a HABTM association. So my question is where is the best place to add the constraint? Art Shayderov You can add uniqueness to join table add_index :users_roles, [ :user_id, :role_id ], :unique

How to set up a typical users HABTM roles relationship

混江龙づ霸主 提交于 2019-11-30 09:41:10
I'm quite new to this and I'm using cancan + devise for my user auth. However I'm not really sure what it means to set up a typical users HABTM roles relationship nor do I really understand what a HABTM relationship is. Can anyone explain it really well or point me to a good tutorial or example? HABTM means has and belongs to many. You basically need a table as a middle man to track multiple id's (called a through table). When referenced as a typical users HABTM roles relationship, they really mean there would be a User model, Role model, users table, roles table, and a roles_users table. Don

How do I remove a single HABTM associated item without deleting the item itself?

冷暖自知 提交于 2019-11-30 03:07:28
How do you remove a HABTM associated item without deleting the item itself? For example, say I have 3 Students that are in a Science class together. How do I remove the Science objects from the StudentsClasses table without deleting the actual Science reference? I'm guessing that Student.Classes.first.delete isn't a good idea. I'm using JavaScript with drag-and-drop for adding and removing, not check boxes. Any thoughts? Michael Sofaer I tend to use has_many :through, but have you tried student.classes.delete(science) I think needing to have the target object, not just the ID, is a limitation

HABTM mongoid following/follower

倾然丶 夕夏残阳落幕 提交于 2019-11-30 01:02:05
Mongoid ships with .push on a habtm, which sets a habtm relationship in both directions. Although delete will #delete an associated record, there's no documented way to delete only a relationship that I have seen. Is there a better way of doing this? Is there a better way of ensuring uniqueness? has_and_belongs_to_many :following, {class_name: 'User', inverse_of: :followers, inverse_class_name: 'User'} has_and_belongs_to_many :followers, {class_name: 'User', inverse_of: :following, inverse_class_name: 'User'} def follow!(user) self.following.push(user) # this pushes the inverse as well self

Rails habtm joins

被刻印的时光 ゝ 提交于 2019-11-29 19:51:53
问题 I have this relationship between categories, products & brands : class Brand < ActiveRecord::Base has_many :products end class Category < ActiveRecord::Base has_and_belongs_to_many :products end class Product < ActiveRecord::Base has_and_belongs_to_many :categories belongs_to :brand end How can I select all categories by specified brand with this relations? I try this but get an error b = Brand.find(1) Category.joins(:products).where(:products => b.products) 回答1: You did the right thing with

rails 3 habtm delete only association

那年仲夏 提交于 2019-11-29 11:14:57
问题 class Company has_and_belongs_to_many :users end class User has_and_belongs_to_many :companies end when i delete a company, what's the best (recommended) way to delete ONLY the associations of the users from that company? (i mean not the actual users, only the associations) 回答1: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many collection.delete will do the trick. 回答2: I prefer the following since it keeps model logic in the model.

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

北慕城南 提交于 2019-11-29 07:03:12
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_id_in][] and I check "A, C" my results are Photo 1 and Photo 2. I only want Photo 1, because I asked

Conditions to paginate for belongsToMany CakePHP 3

妖精的绣舞 提交于 2019-11-29 02:28:22
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->set('_serialize', ['disciplines']); } You'll have to use a query that uses matching or joins to be able