has-and-belongs-to-many

Rails HABTM with polymorphic relationships

ぐ巨炮叔叔 提交于 2019-12-04 13:30:25
问题 I have a table Category which can have many Businesses and Posts . And a Business / Post can have many Categories so I created a polymorphic tabled called CategoryRelationship to break up the many to many relationship. Business model has these relationships: has_many :categories, through: :category_relationships, :source => :category_relationshipable, :source_type => 'Business' has_many :category_relationships CategoryRelationship model has these relationships: attr_accessible :category_id,

How do I query data in CakePHP using HABTM relationships?

会有一股神秘感。 提交于 2019-12-04 06:14:13
I'm working on a CakePHP 1.2 application. I have a model "User" defined with a few HABTM relationships with other tables through a join table. I'm now tasked with finding User information based on the data stored in one of these HABTM tables. Unfortunately, when the query executes, my condition is rejected with an error about a missing table. Upon inspection it seems that CakePHP is not including any of the HABTM tables in the select statement. My User HABTM relationship is as follows: var $hasAndBelongsToMany = array( 'Course' => array( 'className' => 'Course', 'joinTable' => 'course_members'

Rails 4 HABTM custom validation on associations

心不动则不痛 提交于 2019-12-04 05:32:51
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 check_maximum_number_of_roles if self.roles.length > 3 errors.add(:roles, 'Users can only have three

Yii framework Many to Many relationships

自闭症网瘾萝莉.ら 提交于 2019-12-04 01:13:54
What is the method to save and update Many to Many relationship in Yii framework? There is a better implementation as behavior. http://www.yiiframework.com/forum/index.php?/topic/6905-please-test-my-ar-enhancement-automatically-sync-many-many-table-when-calling-save/ Unless you create a model for the table between the two main tables, your only option is to use DAO (Database Access Object) and specify SQLs with it. Have a look at how blog demo accomplishes this task. user318750 use MANY_MANY relationship type to setup many to many connection between Models (An associative table is needed to

update values of checkbox with HABTM relationship — Rails

有些话、适合烂在心里 提交于 2019-12-03 23:11:47
问题 Hey guys I've been using the has_and_belongs_to_many relationship with checkboxes example from the Railscast Episode #17 . I had some problems and now everything is working kind of smoothly except the update button will not work. the edit view looks like so <% form_for :users, :action => 'update' do |f| %> <% for interest in Interest.find(:all) %> <div> <%= check_box_tag "user[interest_ids][]", interest.id, @user.interests.include?(interest) %> <%= interest.name %> </div> <% end %> <p> <%= f

How can I load HABTM-with-foreign-key relationships in my fixtures?

我们两清 提交于 2019-12-03 13:18:21
I have the following two models: School and User, and a HABTM relationship between them, with a join table. In this join table, the foreign key refering to the User table is not called user_id but student_id . class School < ActiveRecord::Base has_and_belongs_to_many :students, :class_name => "User", :join_table => "schools_students", :foreign_key => "student_id" end class User < ActiveRecord::Base has_and_belongs_to_many :studying_schools, :class_name => "School", :join_table => "schools_students", :foreign_key => "school_id" end I would like to create in my Users and Schools fixtures a

acts_as_list with has_and_belongs_to_many relationship

喜你入骨 提交于 2019-12-03 12:08:36
I've found an old plugin called acts_as_habtm_list - but it's for Rails 1.0.0. Is this functionality built in acts_as_list now? I can't seem to find any information on it. Basically, I have an artists_events table - no model. The relationship is handled through those two models specifying :has_and_belongs_to_many How can I specify order in this situation? I'm assuming that you have two models - Artist and Event. You want to have an habtm relationship between them and you want to be able to define an order of events for each artist. Here's my solution. I'm writing this code from my head, but

has_and_belongs_to_many or has_many for user/group relationship?

家住魔仙堡 提交于 2019-12-03 08:46:08
I'm working on a Rails 3.1 app that has the following models: User: class User < ActiveRecord::Base has_and_belongs_to_many :groups has_many :ownerships, :class_name => 'Group' end Group: class Group < ActiveRecord::Base has_and_belongs_to_many :users has_one :owner, :class_name => 'User' end There's a join table between them, and the groups table also has a "user_id" column. I would expect to be able to write this in my groups_controller.rb @group = Group.find(params[:id]) foo = @group.owner but when I do I'm presented with the following error: Mysql2::Error: Unknown column 'users.group_id'

Add record to a has_and_belongs_to_many relationship

雨燕双飞 提交于 2019-12-03 03:10:31
问题 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:

What is the fastest way to create mass HABTM associations in Rails?

[亡魂溺海] 提交于 2019-12-03 02:04:16
I have two tables, with a HABTM relationship in Rails. Something like the following: class Foo &lt ActiveRecord::Base has_and_belongs_to_many :bars end class Bar &lt ActiveRecord::Base has_and_belongs_to_many :foos end Now I have a new Foo object, and want to mass-assign thousands of bars to it, which I've pre-loaded: @foo = Foo.create @bars = Bar.find_all_by_some_attribute(:a) What's the fastest way to do this? I've tried: @foo.bars = @bars @foo.bars &lt&lt @bars And both run really slow, with an entry like the following for each bar : bars_foos Columns (1.1ms) SHOW FIELDS FROM bars_foos SQL