has-and-belongs-to-many

nested form & habtm

橙三吉。 提交于 2019-12-05 15:36:00
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 => params[:group]) end in the new user view, i have access to the User.user_groups object, however

Rails HABTM after_add callback fires before saving primary object

廉价感情. 提交于 2019-12-05 15:24:50
I have two ActiveRecord models having a HABTM relationship with eachother. When I add an AccessUnit through a form that allows zones to be added by checking checkboxes I get an exception that the AccessUnitUpdaterJob can't be enqueued because the access unit passed can't be serialized (due to the fact that the identifier is missing). When manually calling save on the primary object, the issue is resolved but of course this is a workaround and not a proper fix. TLDR; it seems the after_add callback is triggered before the main object is saved. I'm actually unsure if this is a bug in Rails or

Validating length of habtm association without saving

荒凉一梦 提交于 2019-12-05 09:42:25
I have a user model with a HABTM relationship to groups. I do not want a user to be able to be in more than 5 groups, so would like to validate the length of the HABTM relationship. On the edit user page I have a list of checkboxes where the user can select the groups they want to be in (I'm using formtastic for the form). In my users controller I'm calling: @user.update_attributes(params[:user]) which is causing rails to update the associations automatically. In my user model I have the following: def validate if self.groups.length > 5 self.errors.add(:groups, "cannot have more than 5 groups"

Has and belongs to many relationship with multiple databases

老子叫甜甜 提交于 2019-12-05 05:10:54
I have a situation where I have two models, companies and permissions, where companies is in a separate database from my permissions database. This is a has and belongs to many relationship because each company can have many permissions and each permission can belong to many companies. The reason the two databases are split is because the company database runs a high demand production application and the permissions database controls the permissions for another application. With rails, it looks for the join table in the same database as the primary table. For instance, if I do company

ActiveAdmin won't save has many and belongs to many field

天涯浪子 提交于 2019-12-05 03:31:19
I have 2 models. Category and Post. They are connected using a has_many_and_belongs_to_many relationship. I checked in the rails console and the relationship works. I created checkboxes in activeadmin to set the post categories using this form field: f.input :categories, as: :check_boxes, collection: Category.all The problem is when I try to save it because every other field data (title, body, meta infos etc.) is saved, but the category stays the same even if I unchecked it, or checked another too. I am using strong parameters like this: post_params = params.require(:post).permit(:title,:body,

Efficient ActiveRecord has_and_belongs_to_many query

梦想的初衷 提交于 2019-12-05 03:06:40
I have Page and Paragraph models with a has_and_belongs_to_many relation. Given a paragraph_id, I'd like to get all matching pages. e.g.: pages = Paragraph.find(paragraph_id).pages.all However, this takes two queries. It can be done in one query: SELECT "pages".* FROM "pages" INNER JOIN "pages_paragraphs" ON "pages_paragraphs"."page_id" = "pages"."id" WHERE "pages_paragraphs"."paragraph_id" = 123 But can this be done without using find_by_sql without modifications to the page_paragraphs table (e.g. adding an id). Update: My page model looks like this: class Page < ActiveRecord::Base has_and

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

一曲冷凌霜 提交于 2019-12-04 22:33:26
问题 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

CakePHP multiple model conditions in find() with HABTM

旧街凉风 提交于 2019-12-04 17:10:07
My schema has the following relations: User hasMany Transaction belongsTo User Item hasMany Transaction belongsTo Item User hasManyAndBelongsTo Item using Transaction as join table I'd like to return all items of a given item symbol that belong to a given user id . I'm able to retrieve all the items that belong to the user with $this->User->find('all', array( 'conditions' => array( 'User.id' => $userId ) ) ) and I'm able to retrieve all the Items with a given item name with $this->Item->find( 'all', array( 'conditions => array( 'Item.symbol' => $itemSymbol ) ); but if I try to use the

Rails 3: Devise add roles checkboxes to registration form

ぐ巨炮叔叔 提交于 2019-12-04 15:59:31
I'm using Devise for user registration. I've been reading the all famous tutorial about customizing Devise, but can't understand this simple task. I followed his model (HABTM) I want to add a roles check box to the Devise edit form. I don't have a Controller cause Devise doesn't provide one, but managed to add a default role to new users. I was able to display the checkboxes with the correct info checked but can't edit it (it won't save anydata). Do I need a custom controller? if yes, how exactly? I'm new to HABTM relations! My User model class User < ActiveRecord::Base has_and_belongs_to_many

DataMapper has n through Resource DELETE (Remove from association) not working

ぐ巨炮叔叔 提交于 2019-12-04 14:19:48
I'm have this two classes, class User include DataMapper::Resource property :id, Serial property :name, String has n :posts, :through => Resource end class Post include DataMapper::Resource property :id, Serial property :title, String property :body, Text has n :users, :through => Resource end So once I have a new post like: Post.new(:title => "Hello World", :body = "Hi there").save I'm having serious problems to add and remove from the association, like: User.first.posts << Post.first #why do I have to save this as oppose from AR? (User.first.posts << Post.first).save #this just works if