has-and-belongs-to-many

Rails HABTM fields_for – check if record with same name already exists

大兔子大兔子 提交于 2019-12-24 16:29:52
问题 I have a HABTM-relation between the models "Snippets" and "Tags". Currently, when i save a snippet with a few tags, every tag is saved as a new record. Now i want to check if a tag with the same name already exists and if that´s the case, i don´t want a new record, only an entry in snippets_tags to the existing record. How can i do this? snippet.rb: class Snippet < ActiveRecord::Base accepts_nested_attributes_for :tags, :allow_destroy => true, :reject_if => lambda { |a| a.values.all?(&:blank?

Rails routing for has_and_belongs_to_many relationship

怎甘沉沦 提交于 2019-12-24 06:07:48
问题 I'm working on a rails 4 API for a sports team where I have players and teams and I'm struggling a little with rails routing and a has_many relationship. My relationship between players and teams looks like: class Team < ActiveRecord::Base extend Searchable validates :title, presence: true has_and_belongs_to_many :players end class Player < ActiveRecord::Base extend Searchable validates :first_name, presence: true validates :last_name, presence: true has_and_belongs_to_many :teams end I'd

Rails routing for has_and_belongs_to_many relationship

末鹿安然 提交于 2019-12-24 06:07:33
问题 I'm working on a rails 4 API for a sports team where I have players and teams and I'm struggling a little with rails routing and a has_many relationship. My relationship between players and teams looks like: class Team < ActiveRecord::Base extend Searchable validates :title, presence: true has_and_belongs_to_many :players end class Player < ActiveRecord::Base extend Searchable validates :first_name, presence: true validates :last_name, presence: true has_and_belongs_to_many :teams end I'd

SQLite3::ConstraintException: xxxxx.created_at may not be NULL

跟風遠走 提交于 2019-12-24 02:56:25
问题 I have a resque job that creates an artist and associates it with a user. User has_and_belongs_to_many :artists, and Artist has_and_belongs_to_many :users. def self.perform(itunes_id, user_id=nil) artist = Artist.find_by_itunes_id(itunes_id) || lookup_and_create_artist(itunes_id) if user_id && user = User.find(user_id) user.artists << artist user.save! end end user.artists << artist raises this exception: ActiveRecord::StatementInvalid: SQLite3::ConstraintException: artists_users.created_at

CakePHP Containable with HABTM

若如初见. 提交于 2019-12-24 02:42:48
问题 To explain the issue I'm having, I'll use an example. Let's say that I'm building a system where students can sign up for one or more afterschool courses, but a school official has to approve the sign-up in order for it to be valid. So I have these models: Course (belongs to "Teacher", hasAndBelongsToMany "Student") Student (hasAndBelongsToMany "Course") Teacher (hasMany "Course") Now let's say I want to see a list of all of the unapproved sign-ups that are for ninth-graders. That's easy:

How can i use accepts nested attributes with the HABTM?

試著忘記壹切 提交于 2019-12-23 09:47:47
问题 I have two models User and Category. class User < ActiveRecord::Base has_and_belongs_to_many :categories accepts_nested_attributes_for :categories end similarly class Category < ActiveRecord::Base has_and_belongs_to_many :users end i have a requirement where i have to add the categories to the categories table and add the reference so that i can get the categories related to user but if another user enters the same category then i have to make use of the id instead of creating new one. How

HABTM with additional columns for recipe ingredients

有些话、适合烂在心里 提交于 2019-12-23 04:21:27
问题 For my recipe-sharing website, I want to create a database and CakePHP models for the recipes and their ingredients. I created two tables: recipes and ingredients. The third table is a HABTM table for ingredients_recipes, which also stores the amount of ingredients needed for the recipes. How do I create a model for the third table? The third table would look something like this: recipe_id ingredient_id amount measurement_unit I also thought of adding another table for measurement_units to

CakePHP find HABTM

£可爱£侵袭症+ 提交于 2019-12-22 14:51:41
问题 I have 3 models ( User , Message and Tag ) with the following relations: User hasMany Message Message belongsto User Message HABTM Tag Tag HABTM Message If a User is logged in he might want to see all Message tagged with something. $messages = $this->Message->find('all', array( 'conditions' => array("Message.user_id" => $this->uid), 'contain' => array( 'Tag' => array( 'conditions' => array( 'Tag.id' => $activetag['Tag']['id'] ) ) )); However, this find will return ALL messages of that user.

CakePHP HABTM - I don't understand it

妖精的绣舞 提交于 2019-12-22 14:48:10
问题 Asked before but still in the dark. cakePHP HABTM, am I getting it all wrong? Will try again: I have two tables: stores and managers I want to create a new table, managers_stores and use it to keep a complete list of mangers for each store. I understood that each save() action that will provide the full record data (as I intend to do) will result in the deletion of the table rows and only the new record will be kept.. This is an UNDESIRED behavior for my needs. So, how should I manage the

Rails3 + Typus: display habtm relationship as checkboxes

≯℡__Kan透↙ 提交于 2019-12-22 12:16:48
问题 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