has-and-belongs-to-many

Rails3 + Typus: display habtm relationship as checkboxes

杀马特。学长 韩版系。学妹 提交于 2019-12-22 12:15:06
问题 I'm managing my admin backend with Typus I want to add a field with a checkbox for each category. I used this code: Post: fields: default: name, description, user, categories list: name, description, user, categories form: name, description, user, categories relationships: categories But the result is a textfield with a [] inside: how do I solve this? Is there a native way or must I override the field template? 回答1: To show the the checkbox for the fields you can use the existing template of

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

浪尽此生 提交于 2019-12-22 04:37:13
问题 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

How to add multiple many-to-many fields in one form with Rails 4?

折月煮酒 提交于 2019-12-21 23:09:09
问题 I have a form that's got the following association: Course.rb has_and_belongs_to_many :skills Skill.rb has_and_belongs_to_many :courses what I wanna do is allow the person who wants to add a new Course select all the skills from his selected category and be able to add them by using a checkbox. In the view I've done like so: VIEW <%= form_for(@course) do |f| %> <% @skills.each do |s| %> <%= f.check_box :value => s.id %> <%= s.title %><br /> <% end %> <% end %> Sadly this isn't working and I

How to add multiple many-to-many fields in one form with Rails 4?

我的未来我决定 提交于 2019-12-21 22:56:03
问题 I have a form that's got the following association: Course.rb has_and_belongs_to_many :skills Skill.rb has_and_belongs_to_many :courses what I wanna do is allow the person who wants to add a new Course select all the skills from his selected category and be able to add them by using a checkbox. In the view I've done like so: VIEW <%= form_for(@course) do |f| %> <% @skills.each do |s| %> <%= f.check_box :value => s.id %> <%= s.title %><br /> <% end %> <% end %> Sadly this isn't working and I

Simulating has_and_belongs_to_many nested through behavior in Rails 3

让人想犯罪 __ 提交于 2019-12-21 22:20:47
问题 So Rails doesn't have support for :through associations through a habtm relationship. There are plugins out there that will add this in for Rails 2.x, but I'm using Rails 3 / Edge, and only need the association for one particular model. So I thought I'd stump it out myself with the beauty that is Arel. First, the models: class CardSet < ActiveRecord::Base has_and_belongs_to_many :cards, :uniq => true end class Card < ActiveRecord::Base has_and_belongs_to_many :card_sets, :uniq => true has

Rails 3: Devise add roles checkboxes to registration form

我与影子孤独终老i 提交于 2019-12-21 21:54:40
问题 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

BelongsToMany relation. How to get unique rows

五迷三道 提交于 2019-12-21 17:35:07
问题 I have next 'member_companies' table in DB: And in model Member has a relation : public function companies() { return $this->belongsToMany(Company::class, 'member_companies'); } And it return me all companies with dublicates. For example, Member::find(238)->companies->pluck('id') return [ 6, 5, 7, 2, 10, 8, 4, 13, 14, 10, 8, 13 ] But I want only unique items. Like [ 6, 5, 7, 2, 10, 8, 4, 13, 14, ] How can I do it with eloquent relations? 回答1: You should use the unique() method in your

acts_as_list with has_and_belongs_to_many relationship

时间秒杀一切 提交于 2019-12-21 03:56:20
问题 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? 回答1: 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

Cakephp retrieving HABTM which Conditions

て烟熏妆下的殇ゞ 提交于 2019-12-20 14:48:47
问题 I'm using cakephp and would like to display all Submissions which are part of Category 'X' I have 4 tables with a HABTM relationship. Users -> (haveMany) -> Submissions <-> (hasAndBelongsToMany) <-> Categories but I would like to do so using the $this->paginate() and for each submission I would like to display the user who posted the submission. User Table Id | Name -----+------------------- 1 | User 1 2 | User 2 Submission Table Id | Name | User_id -----+-------------------+-------------- 1

Cakephp retrieving HABTM which Conditions

前提是你 提交于 2019-12-20 14:48:29
问题 I'm using cakephp and would like to display all Submissions which are part of Category 'X' I have 4 tables with a HABTM relationship. Users -> (haveMany) -> Submissions <-> (hasAndBelongsToMany) <-> Categories but I would like to do so using the $this->paginate() and for each submission I would like to display the user who posted the submission. User Table Id | Name -----+------------------- 1 | User 1 2 | User 2 Submission Table Id | Name | User_id -----+-------------------+-------------- 1