has-and-belongs-to-many

Scope for Self-joining HABTM Association

☆樱花仙子☆ 提交于 2019-12-12 03:22:01
问题 With Rails 3.1.3 on Ruby 1.9.2 , I have: class User < ActiveRecord::Base has_and_belongs_to_many :states end class State < ActiveRecord::Base has_and_belongs_to_many :users end A User is associated with many State s, and a State with many User s. Given a User , I want to find all other User s who are in any of her states. I tried: class User < ActiveRecord::Base scope :in_state, lambda { |states| joins(:states).where(:states => states) } end User.in_state(current_user.states).all With this

cakePHP HABTM, am I getting it all wrong?

依然范特西╮ 提交于 2019-12-12 02:43:07
问题 I understood that every new row, causes the deletion of the rows that were there before? What is the idea behind it? I don't believe that it is .. So, what am i getting wrong? Edit A I have a form that adds a store to the Stores table. the store have a column named owner_id which is associated to the Users table through a belongsTo relationship. There is also a table named stores_users that supposed to store the mangers for each store, using the HABTM relationship. For this table there is a

Rails 3 has_and_belongs_to_many association: how to assign related objects without saving them to the database

你说的曾经没有我的故事 提交于 2019-12-11 18:08:07
问题 Working with an has_and_belongs_to_many_association class Category has_and_belongs_to_many :projects end I would would like to use a before_filter to set projects before saving categories before_filter :set_projects, :only => [:create, :update] def set_projects @category.assign_attributes({projects: Project.all}) end This works well, except when the category cannot be saved and there is a rollback. The projects are still updated in database. Why this line @category.assign_attributes({projects

CakePHP multi-HABTM associations with pagination

爱⌒轻易说出口 提交于 2019-12-11 17:54:01
问题 For an e-commerce app that I'm building I am using CakePHP. I have created the db and afterwards baked my app with cake bake . My models are all linked up properly as follows: Product hasMany CategoryProduct,ImagesProduct Category hasMany CategoryProduct CategoryProduct belongsTo Product,Category Image hasMany ImagesProduct ImagesProduct belongsTo Product,Image I have tried in various ways to obtain a paginated view of products dependent of the category_id with no succes. I have managed to

CakePHP ignores beforeSave methods when saving HABTM join model

若如初见. 提交于 2019-12-11 11:35:21
问题 So I have three models: Coach: var $hasAndBelongsToMany = array("Tour") Tour: var $hasAndBelongsToMany = array("Coach") CoachesTour: var $belongsTo = array("Tour", "Coach") There is an HABTM association between Coach and Tour, and it should use the CoachesTour as the join model. I'm using scaffold. When modifying a Tour, if I add a new CoachesTour to it, the beforeSave method of CoachesTour isn't called. It seems as if the records of the join model are being inserted as SQL statements,

Views in a has_many :through relationship

陌路散爱 提交于 2019-12-11 06:38:36
问题 I've been tinkering with this for some time now, and can't seem to figure this out. It's probably something simple, but here goes: I have a has_many :trough relationship between 'laminate', and 'standards' with a joined model 'standardization'. Standard.rb class Standard < ActiveRecord::Base attr_accessible :description, :name has_many :standardizations has_many :laminates, :through => :standardizations end Standardization.rb class Standardization < ActiveRecord::Base attr_accessible

CakePHP hasAndBelongsToMany vs hasMany through

↘锁芯ラ 提交于 2019-12-11 05:56:10
问题 This is only my 3rd CakePHP app, and the first to require a HABTM type association. I'm working with Cake 2.1, in which there was a change to HABTM parameters allowing for extra information to be saved by setting the 'unique' key to 'keepExisting'. In a normal HABTM association you have a table with 2 fields, each foreign keys. I think I need one with 3 foreign keys. Is that possible by using the 'unique' key, or should I use a hasMany through association? Here's my schema: /* repair_orders *

Rails : assign multiple params via form_for check_box for HABTM relationship

白昼怎懂夜的黑 提交于 2019-12-11 02:49:05
问题 I'm a rails noob working on my first own project. I got two models linked using a has_and_belongs_to_many relationship : Wine and Shop . To make it simple a wine can be sold in different shops and a specific shop can sell many different wines. Here are the models : class Shop < ActiveRecord::Base has_and_belongs_to_many :wines end class Wine < ActiveRecord::Base has_and_belongs_to_many :shops end My goal is to make a form to create instances of Wine including the Shops where the wine can be

HABTM with self requires 2x the rows in join table?

旧时模样 提交于 2019-12-11 01:17:52
问题 I'm trying to build a CMS with Nodes as the main model. Each Node belongsTo a NodeType , and every Node can be related to any/every other Node . So - thought this called for HABTM: //Node model public $hasAndBelongsToMany = array( 'AssociatedNode' => array( 'className' => 'Node', 'foreignKey' => 'node_id', 'associationForeignKey' => 'associated_node_id', 'joinTable' => 'node_associations' ) ); The problem is, it seems like the only way it works is if I have TWO rows for each association.

Rails 3. HABTM form select drop down menu

空扰寡人 提交于 2019-12-10 21:16:46
问题 I have an invoice form. This is a simplified version: so it has line items where you select a drop down menu of product names. This is working well: So the invoice-line_item relations is this: invoice has_many line_items and line_item belongs to invoice. line_item belongs to item and item has_many line_items. I have the items, line_items and invoice setup correctly. But now I want to add taxes to the line items. So I created a line_items_taxes table to create a HABTM relationship between line