has-and-belongs-to-many

Trying to create a scope in rails 3 for has habtm relationship

别来无恙 提交于 2019-12-10 20:38:01
问题 I'm trying to do a basic search on an active record but am having trouble because of the has_and_belongs_to_many relationship. Below are the records and scopes I've created. Class Section << ActiveRecord::Base has_and_belongs_to_many :teachers attr_accessible :department, :class_size scope :department_scope, lambda { |dept| where("department = ?", dept) } scope :teacher_fname_scope, lambda { |n| joins(:teachers) & Teacher.has_first_name(n) } end Class Teacher << ActiveRecord::Base has_and

HABTM association with Strong Parameters is not saving user in Rails 4

孤者浪人 提交于 2019-12-10 16:18:18
问题 User model: has_and_belongs_to_many :events Event model: has_and_belongs_to_many :users Users controller: params.require(:user).permit(:role, {:event_ids => []}) Events controller: params.require(:event).permit(:subject, :location, :date, :time, :all_day, :reminder, :notes, {:users_ids => []}) My form: <%= simple_form_for(@event) do |f| %> <%= f.input :subject %> <%= f.input :location %> <%= f.input :date %> <%= f.input :time %> <%= f.association :users %> <%= f.input :all_day %> <%= f.input

CakePHP defining additional fields in HABTM/ManyToMany relationship?

主宰稳场 提交于 2019-12-10 10:42:44
问题 Basically I would like to recreate this in CakePHP: Many to many relationships with additional data on the relationship or in other words, my intermediary table to have extra fields defining the type of relationship. How do I set this up in CakePHP? You can use as example: model company: id name person_id model person: id name model company_person: company_id person_id position thanx 回答1: if you want to have additional data in HABTM, use 2 hasMany relationships instead. So if A HABTM B, then

Rails 4 - best_in_place gem and many-to-many association

◇◆丶佛笑我妖孽 提交于 2019-12-10 09:44:53
问题 I am struggling to add the ability to update a contact languages using best_in_place (a jQuery inplace-editor gem). I have a contact object, a language object and a has_and_belongs_to_many :languages relationship between contacts and languages. Therefore, a has_and_belongs_to_many implied a join table between the contacts and the languages tables. To sum up quickly, I have something like this: contacts contacts_languages languages +------+--------+ +------------+-------------+ +------+-------

nested form & habtm

流过昼夜 提交于 2019-12-10 09:25:06
问题 I am trying to save to a join table in a habtm relationship, but I am having problems. From my view, I pass in a group id with: <%= link_to "Create New User", new_user_url(:group => 1) %> # User model (user.rb) class User < ActiveRecord::Base has_and_belongs_to_many :user_groups accepts_nested_attributes_for :user_groups end # UserGroups model (user_groups.rb) class UserGroup < ActiveRecord::Base has_and_belongs_to_many :users end # users_controller.rb def new @user = User.new(:user_group_ids

rails prevent deletion of child unless parent is being deleted also

我是研究僧i 提交于 2019-12-10 03:06:03
问题 in Ruby on Rails 4, let's say a parent has many children. When the parent is deleted, the children must also be deleted. Other than that, the child shall not be deleted unless it is an orphan. How to do that? I tried with the following class Parent < ActiveRecord::Base has_many :children, inverse_of: :parent, dependent: :destroy end class Child < ActiveRecord::Base belongs_to :parent, inverse_of: :children before_destroy :checks private def checks not parent # true if orphan end end With the

Rails 4 HABTM custom validation on associations

空扰寡人 提交于 2019-12-09 17:41:16
问题 I've got a simple scenario, but I can't seem to find any proposed solutions that apply to Rails 4. I want to simply add a custom validator that checks the amount of stored associations between my HABTM association. Easier said and done, to my surprise? I've searched for a solution but only end up with answers for older versions of Rails it seems. I've got the following: class User < ActiveRecord::Base has_and_belongs_to_many :roles after_save :check_maximum_number_of_roles . . . private def

Associate two already existing objects using Has And Belongs To Many

我的梦境 提交于 2019-12-08 11:33:18
问题 I'm making an App in Rails to show anime, these animes has and belongs to many languages, so I made a HABTM association: class Anime < ActiveRecord::Base has_and_belongs_to_many :languages end class Language < ActiveRecord::Base has_and_belongs_to_many :animes end Now I don't know how can I make associations between them, I've created many Languages' records to use them, for example, Language with ID 1 is English, Language with ID 2 is Spanish, etc... And I want to just make the associations

how use multiple conditions to get data from 2 HABTM tables in Cakephp 2.x

ぐ巨炮叔叔 提交于 2019-12-08 07:44:25
问题 I'm trying to get only those products which have specific brand and category. in Product model i used this query $products = $this->find('all', array( 'joins' => array( array('table' => 'brands_products', 'alias' => 'BrandsProducts', 'type' => 'INNER', 'conditions' => array( 'BrandsProducts.brand_id' => $brandId, 'BrandsProducts.product_id = Product.id' ) ) ), 'group' => 'Product.id' )); By using above query i can get only those products which has specific brand id. Exactly i don't know how

Creating multiple records in a HABTM relationship using a collection_select - Rails

a 夏天 提交于 2019-12-08 05:47:42
问题 I have a has_and_belongs_to_many relationship between workouts and groups. I have a collection_select in groups that I'm using to add workouts to groups. The problem is that I can only change the one record in the HABTM table, so I can only add one record, then edit that record. How do I add additional records? Any ideas? Here is some code: show.html.erb: <%= form_for(@group) do |f| %> <%= f.collection_select 'workout_ids', Workout.all, :id, :name, { :include_blank => ""} %> <%= f.submit %> <