Search Results

Search found 73 results on 3 pages for 'habtm'.

Page 1/3 | 1 2 3  | Next Page >

  • CakePHP HABTM: Editing one item casuses HABTM row to get recreated, destroys extra data

    - by leo-the-manic
    I'm having trouble with my HABTM relationship in CakePHP. I have two models like so: Department HABTM Location. One large company has many buildings, and each building provides a limited number of services. Each building also has its own webpage, so in addition to the HABTM relationship itself, each HABTM row also has a url field where the user can visit to find additional information about the service they're interested and how it operates at the building they're interested in. I've set up the models like so: <?php class Location extends AppModel { var $name = 'Location'; var $hasAndBelongsToMany = array( 'Department' => array( 'with' => 'DepartmentsLocation', 'unique' => true ) ); } ?> <?php class Department extends AppModel { var $name = 'Department'; var $hasAndBelongsToMany = array( 'Location' => array( 'with' => 'DepartmentsLocation', 'unique' => true ) ); } ?> <?php class DepartmentsLocation extends AppModel { var $name = 'DepartmentsLocation'; var $belongsTo = array( 'Department', 'Location' ); // I'm pretty sure this method is unrelated. It's not being called when this error // occurs. Its purpose is to prevent having two HABTM rows with the same location // and department. function beforeSave() { // kill any existing rows with same associations $this->log(__FILE__ . ": killing existing HABTM rows", LOG_DEBUG); $result = $this->find('all', array("conditions" => array("location_id" => $this->data['DepartmentsLocation']['location_id'], "department_id" => $this->data['DepartmentsLocation']['department_id']))); foreach($result as $row) { $this->delete($row['DepartmentsLocation']['id']); } return true; } } ?> The controllers are completely uninteresting. The problem: If I edit the name of a Location, all of the DepartmentsLocations that were linked to that Location are re-created with empty URLs. Since the models specify that unique is true, this also causes all of the newer rows to overwrite the older rows, which essentially destroys all of the URLs. I would like to know two things: Can I stop this? If so, how? And, on a less technical and more whiney note: Why does this even happen? It seems bizarre to me that editing a field through Cake should cause so much trouble, when I can easily go through phpMyAdmin, edit the Location name there, and get exactly the result I would expect. Why does CakePHP touch the HABTM data when I'm just editing a field on a row? It's not even a foreign key!

    Read the article

  • CakePHP hasAndBelongsToMany (HABTM) Delete Joining Record

    - by Jason McCreary
    I have a HABTM relationship between Users and Locations. Both Models have the appropriate $hasAndBelongsToMany variable set. When I managing User Locations, I want to delete the association between the User and Location, but not the Location. Clearly this Location could belong to other users. I would expect the following code to delete just the join table record provided the HABTM associations, but it deleted both records. $this->Weather->deleteAll(array('Weather.id' => $this->data['weather_ids'], false); However, I am new to CakePHP, so I am sure I am missing something. I have tried setting cascade to false and changing the Model order with User, User-Weather, Weather-User. No luck. Thanks in advance for any help.

    Read the article

  • CakePHP HABTM Filtering

    - by James Haigh
    Hi, I've got two tables - users and servers, and for the HABTM relationship, users_servers. Users HABTM servers and vice versa. I'm trying to find a way for Cake to select the servers that a user is assigned to. I'm trying things like $this->User->Server->find('all'); which just returns all the servers, regardless of whether they belong to the user. $this->User->Server->find('all', array('conditions' => array('Server.user_id' => 1))) just gives an unknown column SQL error. I'm sure I'm missing something obvious but just need someone to point me in the right direction. Thanks!

    Read the article

  • HABTM selection seemingly ignores joinTable

    - by TheCapn
    I'm attempting to do a HABTM relationship between a Users table and Groups table. The problem is, that I when I issue this call: $this->User->Group->find('list'); The query that is issued is: SELECT [Group].[id] AS [Group__id], [Group].[name] AS [Group__name] FROM [groups] AS [Group] WHERE 1 = 1 I can only assume at this point that I have defined my relationship wrong as I would expect behavior to use the groups_users table that is defined on the database as per convention. My relationships: class User extends AppModel { var $name = 'User'; //...snip... var $hasAndBelongsToMany = array( 'Group' => array( 'className' => 'Group', 'foreignKey' => 'user_id', 'associationForeignKey' => 'group_id', 'joinTable' => 'groups_users', 'unique' => true, ) ); //...snip... } class Group extends AppModel { var $name = 'Group'; var $hasAndBelongsToMany = array ( 'User' => array( 'className' => 'User', 'foreignKey' => 'group_id', 'associationForeignKey' => 'user_id', 'joinTable' => 'groups_users', 'unique' => true, )); } Is my understanding of HABTM wrong? How would I implement this Many to Many relationship where I can use CakePHP to query the groups_users table such that a list of groups the currently authenticated user is associated with is returned?

    Read the article

  • Edit of self referencing HABTM in cakephp works, sometimes

    - by Odegard
    I'm using a self referencing HABTM model with Participants. You sign up for an event and when you log in to your reservation/profile you see a list of other participants and you can choose to add yourself and others into various groups; share hotel room, share transportation from airport etc. What I've managed so far: 1) In my profile I see the list of all other participants with checkboxes. Great so far. 2) Adding another participant works fine. Next time I edit, the participant I added is shown as checked. 3) Removing another participant works fine too as long as you still have checked participants before you submit! Again, with words: There are 3 participants. I'm logged in as one of them, and I see the 2 other people on the participants list. I choose to check both of them. This works fine (always). Later I choose to remove one of them (by unchecking the checkbox and hitting submit). This also works fine (always). If I want to remove the last checkbox... nothing is updated (always!). What's curious is that I can add and remove any odd combination of participants and it will always work UNLESS I choose to remove every participants in one go (removing a one and only participant is a special case of "remove all checked participants"). As far as I know, HABTMs work by first deleting all relations, then re-saving them. I can see that in my tables when I remove, add, remove, add the same participant over and over again - the id on the HABTM table is always increasing. When I deselect all participants at once, however, the relations are not updated. The ids stay the same, so it's like the save never happened. This behaviour is so specific and peculiar, I have a feeling I'm missing something obvious here. Anyway, here's the relevant code: Model class Participant extends AppModel { var $hasAndBelongsToMany = array( 'buddy' = array( 'className' = 'Participant', 'joinTable' = 'participants_participants', 'foreignKey' = 'participant_id', 'associationForeignKey' = 'buddy_id', 'unique' = true, ) ); Controller function edit($id = null) { if (!$id && empty($this-data)) { $this-Session-setFlash(__('Invalid Participant', true)); $this-redirect(array('action'='index')); } if (!empty($this-data)) { if ($this-Participant-saveAll($this-data)) { $this-Session-setFlash(__('The Participant has been saved', true)); $this-redirect(array('action'='index')); } else { $this-Session-setFlash(__('The Participant could not be saved. Please, try again.', true)); } } if (empty($this-data)) { $this-data = $this-Participant-read(null, $id); } // Fetching all participants except yourself $allParticipants = $this-Participant-find('list', array('conditions' = array('participant.id ' = $id))); // Fetching every participant that has added you to their list $allBuddies = $this-Participant-ParticipantsParticipant-find('list', array( 'conditions' = array('buddy_id' = $id), 'fields' = 'ParticipantsParticipant.participant_id', 'order' = 'ParticipantsParticipant.participant_id ASC' )); $this-set(compact('allParticipants','allBuddies')); } View echo $form-create('Participant'); echo $associations-habtmCheckBoxes($allParticipants, $this-data['buddy'], 'buddy', 'div', '\'border: 1px solid #000;\'', '\'border: 1px solid #000;\''); echo $form-end('Submit'); I'm using a slightly modified helper, habtmCheckBoxes, found here: http://cakeforge.org/snippet/detail.php?type=snippet&id=190 It works like this: function habtmCheckBoxes($rows=array(), $selectedArr=array(), $modelName, $wrapTag='p', $checkedDiv, $uncheckedDiv) {}

    Read the article

  • HABTM and belongsTo at the same join, cakePhp

    - by Cynthia
    Hello everyone. I have a model Fix with a relationship HABTM Device model. Device model has a belongsTo to Device_type model, like this, for only getting the device type name: var $belongsTo = array('Device_type'=>array('fields'=>'name')); So, I need every Fix, its devices and its Device_types. When I make a Fix->find('all', array('recursive' => 2)) I expect to get every Device related to Fix (this works ok) and ALSO for every device, its Device_type.name (which is not working). This is what I get instead for every Device in the result (an empty array): ["Device_type"]=> array(0) { } Besides this, when I make this query for testing: Fix->Device->find('all'), it returns the current Device_type.names for every device related to fixes, which means models are related propertly. Any help? Thanks.

    Read the article

  • nested form & habtm

    - by brewster
    so 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 when i submit the form, not only does it not save into my join table (user_groups_users), but the object is no longer there. all the other objects & attributes of my User object are persistent except for the user group. i just started learning rails, so maybe i am missing something conceptually here, but i have been really struggling with this.

    Read the article

  • Rails HABTM Scoping to current subdomain

    - by Rabbott
    I have the following associations: class User < ActiveRecord::Base has_and_belongs_to_many :brands, :join_table => 'brands_users' has_and_belongs_to_many :companies, :join_table => 'companies_users' end class Brand < ActiveRecord::Base belongs_to :company has_and_belongs_to_many :users, :join_table => 'brands_users' end class Company < ActiveRecord::Base has_and_belongs_to_many :users, :join_table => 'companies_users' has_many :brands, :order => :name end While editing a user I am using the checkbox list of brands. So that I can assign users 'access' to brands, the brands that show up are only the brands that belong to the current company (defined by the subdomain [using subdomain_fu]). The problem I am running into is that when using the default HABTM functionality and the checkbox list, upon save, Rails removes ALL user-brand associations, then re-adds just the ones for the form I just submitted.. How do I scope that to only remove associations of brands who belong to the current company, defined in the subdomain?

    Read the article

  • Cakephp, Retreive Data for HABTM Models using find

    - by user298079
    I am new to cakephp and I'm trying to accomplish something that should be relatively easy. I have 2 models projects & categories bind by HABTM relationship. I am trying to perform the following query - find all projects that belong to a category $projects = $this->Project->find('all', array('conditions' => array('Category.slug' => $category))); However when I'm doing so it generates an SQL error: SQL Error: 1054: Unknown column 'Category.slug' in 'where clause' What am I doing wrong??

    Read the article

  • Rails Habtm with a field

    - by moshimoshi
    Hello, Is that possible to have a field in a has and belongs to many table? Just like favorite: create_table :messages_users, :id => false, :force => true do |t| t.integer :message_id, :null => false t.integer :user_id, :null => false t.boolean :favorite, :null => false t.timestamps end I saw timestamps works well, thanks to ActiveRecord. But when I try to add favorite into the table and then I try: Message.first.users << User.first Then I get this error message: ActiveRecord::StatementInvalid: SQLite3::SQLException: messages_users.favorite may not be NULL: INSERT INTO "messages_users" ("created_at", "message_id", "updated_at", "user_id") VALUES ('''2010-05-27 06:07 :50.721512''', 1, '''2010-05-27 06:07:50.721512''', 1) I would like to use a habtm, I don't like has_many foo though bar association :) Is that possible? Thanks a lot.

    Read the article

  • Rails 3: habtm migration, primary key issue

    - by Brian Wigginton
    I'm trying to setup a migration file for a habtm relationship, however when I run the migration I'm getting the following error: Primary key is not allowed in a has_and_belongs_to_many join table (parts_vehicles). Here is my migration file (20110111035950_create_parts_vehicles.rb): class CreatePartsVehiclesJoinTable < ActiveRecord::Migration def self.up create_table :parts_vehicles, :id => false do |t| t.integer :part_id t.integer :vehicle_id end end def self.down drop_table :parts_vehicles end end The documentation example states to use :id => false to disable a primary key from being generated, but I'm still getting the error.

    Read the article

  • Accessing individual HABTM records in a form

    - by Pichan
    I'm building a form in my CakePHP project that lets you edit a company's information. Among all other things, every company has at least one geographical area in which the company operates, but it may have more. The areas are selected individually using select dropdowns. The relationship between companies and areas is HABTM, because I need to be able to change the amount of associated areas without modifying the database. Currently the associations and corresponding data are handled separately, which isn't really a problem but I was wondering how it could be done using as much Cake's own 'automagic' functionality as possible?

    Read the article

  • Cakephp, Retreive Data for HABTM Models using conditional find

    - by ion
    There are 2 Models: Project & Category that are bind with HABTM relationship. I would like to perform a search from projects controller that can do the following: FIND all DISTINCT Project.scedule WHERE Category.slug != 'uncategorised' Apologies for the syntax, I'm no sequel expert. What I have managed to do is to retrieve all projects that do not belong to Category uncategorised into an array however I'm not sure as to how to search again the array result for DISTINCT Project.schedule values (needed to fill out a form drop down) I hope I made myself clear.

    Read the article

  • HABTM checking for match of latest 3

    - by user333614
    Here's an interesting one for you folks... I have a HABTM (has_and_belongs_to_many) relationship between "Dogs" and "Trips". My goal is to find two result sets: 1) Dogs that have been on at least 1 of the last 3 trips and call that @dogs_current 2) Dogs that have NOT been on any of the last 3 trips and call that @dogs_old I found that I can find what the last 3 trips are by doing this in the Trip model: named_scope :last3, :order => 'date DESC', :limit => 3 But not sure how to use that list get 1 and 2. This hack works, but it seems ugly. There must be a better way! :) @dogs_current = [] @dogs_old = [] @dogs.each do |dog| if (Trip.last3 - dog.trips).size < 3 then @dogs_current << dog else @dogs_old << dog end end Any ideas? Thanks! -Cam

    Read the article

  • Making HABTM relationships unique in CakePHP

    - by Andrea
    I have two models, called Book and Tag, which are in a HABTM relationship. I want a couple (book, tag) to be saved only once. In my models I have var $hasAndBelongsToMany = array( 'Tag' => array( 'className' => 'Tag', 'joinTable' => 'books_tags', 'foreignKey' => 'book_id', 'associationForeignKey' => 'tag_id', 'unique' => true ) ); and viceversa, but the Unique flag does not help me; I can still save two times the same couple. How do I do this in CakePHP? Should I declare the couple (book, tag) unique in the database directly, or will this make CakePHP go nuts? Is there a Cakey way to handle this situation? EDIT: I tried making the couple unique with the query (I'm using MySQL) ALTER TABLE books_tags ADD UNIQUE (book_id,tag_id); but this does not work well. When I save more than one tag at a time, everything goes well if all the couples are new. If at least one of the couples is repeated, CakePHP fails to do the whole operation, so it does not save ANY new couple (not even the good ones).

    Read the article

  • rails HABTM versus view (formtastic)

    - by VP
    I have two models: The model NetworkObject try to describe "hosts". I want to have a rule with source and destination, so i'm trying to use both objects from the same class since it dont makes sense to create two different classes. class NetworkObject < ActiveRecord::Base attr_accessible :ip, :netmask, :name has_many :statements has_many :rules, :through =>:statements end class Rule < ActiveRecord::Base attr_accessible :active, :destination_ids, :source_ids has_many :statements has_many :sources, :through=> :statements, :source=> :network_object has_many :destinations, :through => :statements, :source=> :network_object end To build the HABTM i did choose the Model JOIN. so in this case i created a model named Statement with: class Statement < ActiveRecord::Base attr_accessible :source_id, :rule_id, :destination_id belongs_to :network_object, :foreign_key => :source_id belongs_to :network_object, :foreign_key => :destination_id belongs_to :rule end The problem is: is right to add two belongs_to to the same class using different foreign_keys? I tried all combinations like: belongs_to :sources, :class_name => :network_object, :foreign_key => :source_id but no success.. anything that i am doing wrong?

    Read the article

  • cakephp paginate multiple habtm

    - by izmanromli
    hi guys, i have multiple habtm like these : // User model var $hasMany = array('Post'); // Post model var $hasAndBelongsToMany = array('Category', 'Tag'); // Category model var $hasAndBelongsToMany = array('Post'); // Tag model var $hasAndBelongsToMany = array('Post'); I tried to fetch all post along with its user and tags (within a certain category), somehow if i fetch tags, the result was wrong. $this->paginate = array ( 'Post' => array ( 'limit' => 2, 'fields' => array( 'Post.title', 'Post.content', 'Post.slug', 'Post.created', 'Tag.name', 'User.username', 'User.created', 'User.post_count', 'User.avatar_file_name'), 'joins' => array ( array( 'table' => 'categories_posts', 'alias' => 'CategoriesPost', 'type' => 'inner', 'conditions'=> array('CategoriesPost.post_id = Post.id') ), // FETCH USER array( 'table' => 'users', 'alias' => 'User', 'type' => 'inner', 'conditions'=> array('Post.user_id = User.id') ), // FETCH TAGS array( 'table' => 'posts_tags', 'alias' => 'PostsTag', 'type' => 'inner', 'conditions'=> array('PostsTag.post_id = Post.id') ), array( 'table' => 'tags', 'alias' => 'Tag', 'type' => 'inner', 'conditions'=> array('Tag.id = PostsTag.tag_id') ), array( 'table' => 'categories', 'alias' => 'Category', 'type' => 'inner', 'conditions'=> array('Category.id = CategoriesPost.category_id', 'Category.slug' => $slug) ) ) ) ); $posts = $this->paginate(); could anyone gimme a solution since i'm a newbie? many thanks...

    Read the article

  • Dynamic select menu Rails, Javascript HABTM

    - by Jack
    Hi, I am following a tutorial in one of Ryan Bates' Railscasts here. Basically I want a form where there are 2 drop down menus, the contents of one are dependent on the other. I have Years and Courses, where Years HABMT Courses and Courses HABTM Years. In the tutorial, the javascript is as follows: var states = new Array(); <% for state in @states -%> states.push(new Array(<%= state.country_id %>, '<%=h state.name %>', <%= state.id %>)); <% end -%> function countrySelected() { country_id = $('person_country_id').getValue(); options = $('person_state_id').options; options.length = 1; states.each(function(state) { if (state[0] == country_id) { options[options.length] = new Option(state[1], state[2]); } }); if (options.length == 1) { $('state_field').hide(); } else { $('state_field').show(); } } document.observe('dom:loaded', function() { countrySelected(); $('person_country_id').observe('change', countrySelected); }); Where I guess country has many states and state belongs to country. I think what I need to do is edit the first for statement to somehow loop through all of the courses for each year_id, but don't know how to do this. Any ideas? Thanks

    Read the article

  • How to get HABTM associated data using hasOne binding

    - by Moin
    Hi, I am following example documented at http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM I am trying to retrieve associated data using hasOne. I created 3 tables posts, tags and posts_tags. I wrote following code to debug Posts. $this->Post->bindModel(array( 'hasOne' => array( 'PostsTag', 'FilterTag' => array( 'className' => 'Tag', 'foreignKey' => false, 'conditions' => array('FilterTag.id = PostsTag.tag_id') )))); $output=$this->Post->find('all', array( 'fields' => array('Post.*') )); debug($output); I was expecting output something like below. Array ( 0 => Array { [Post] => Array ( [id] => 1 [title] => test post 1 ) [Tag] => Array ( [0] => Array ( [id] => 1 [name] => php ) [1] => Array ( [id] => 2 [name] => javascript ) [2] => Array ( [id] => 3 [name] => xml ) ) } But my output do not have Tags at all. Here is what I got. Array ( [0] => Array ( [Post] => Array ( [id] => 1 [title] => test post1 ) ) [1] => Array ( [Post] => Array ( [id] => 2 [title] => test post2 ) ) ) How do I get associated tags along with the post. I know I am missing something, but unable to figure out. Any help would be highly appreciated.

    Read the article

  • habtm multiple times with the same model

    - by Ermin
    I am trying to model a publications. A publication can have multiple authors and editors. Since it is possible that one person is an author of one publication and an editor of another, no separate models for Authors and Editors: class Publication < ActiveRecord::Base has_and_belongs_to_many :authors, :class_name=>'Person' has_and_belongs_to_many :editors, :class_name=>'Person' end The above code doesn't work, because it uses the same join table. Now I now that I can specify the name of the join table, but there is a warning in the API documentation is a warning about that which I don't understand: :join_table: Specify the name of the join table if the default based on lexical order isn’t what you want. WARNING: If you’re overwriting the table name of either class, the table_name method MUST be declared underneath any has_and_belongs_to_many declaration in order to work.

    Read the article

  • Double join with habtm in ActiveRecord

    - by Daniel Huckstep
    I have a weird situation involving the need of a double inner join. I have tried the query I need, I just don't know how to make rails do it. The Data Account (has_many :sites) Site (habtm :users, belongs_to :account) User (habtm :sites) Ignore that they are habtm or whatever, I can make them habtm or has_many :through. I want to be able to do @user.accounts or @account.users Then of course I should be able to do @user.accounts < @some_other_account And then have @user.sites include all the sites from @some_other_account. I've fiddled with habtm and has_many :through but can't get it to do what I want. Basically I need to end up with a query like this (copied from phpmyadmin. Tested and works): SELECT accounts.* FROM accounts INNER JOIN sites ON sites.account_id = accounts.id INNER JOIN user_sites ON sites.id = user_sites.site_id WHERE user_sites.user_id = 2 Can I do this? Is it even a good idea to have this double join? I am assuming it would work better if users had the association with accounts to begin with, and then worry about getting @user.sites instead, but it works better for many other things if it is kept the way it is (users <- sites).

    Read the article

  • How to create a rails habtm that deletes/destroys without error?

    - by Bradley
    I created a simple example as a sanity check and still can not seem to destroy an item on either side of a has_and_belongs_to_many relationship in rails. Whenever I try to delete an object from either table, I get the dreaded NameError / "uninitialized constant" error message. To demonstrate, I created a sample rails app with a Boy class and Dog class. I used the basic scaffold for each and created a linking table called boys_dogs. I then added a simple before_save routine to create a new 'dog' any time a boy was created and establish a relationship, just to get things setup easily. dog.rb class Dog < ActiveRecord::Base has_and_belongs_to_many :Boys end boy.rb class Boy < ActiveRecord::Base has_and_belongs_to_many :Dogs def before_save self.Dogs.build( :name => "Rover" ) end end schema.rb ActiveRecord::Schema.define(:version => 20100118034401) do create_table "boys", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "boys_dogs", :id => false, :force => true do |t| t.integer "boy_id" t.integer "dog_id" t.datetime "created_at" t.datetime "updated_at" end create_table "dogs", :force => true do |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end end I've seen lots of posts here and elsewhere about similar problems, but the solutions are normally using belongs_to and the plural/singular class names being confused. I don't think that is the case here, but I tried switching the habtm statement to use the singular name just to see if it helped (with no luck). I seem to be missing something simple here. The actual error message is: NameError in BoysController#destroy uninitialized constant Boy::Dogs The trace looks like: /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:105:in const_missing' (eval):3:indestroy_without_callbacks' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/callbacks.rb:337:in destroy_without_transactions' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:229:insend' ... Thanks.

    Read the article

  • Rails 3 HABTM Strange Association: Project and Employee in a tree.

    - by Mauricio
    Hi guys I have to adapt an existing model to a new relation. I have this: A Project has many Employees. the Employees of a Project are organized in some kind of hierarchy (nothing fancy, I resolved this adding a parent_id for each employee to build the 'tree') class Employee < AR:Base belongs_to :project belongs_to :parent, :class_name => 'Employee' has_many :childs, :class_name => 'Employee', :foreign_column => 'parent_id' end class Project < AR:Base has_many :employees, end That worked like a charm, now the new requirement is: The Employees can belong to many Projects at the same time, and the hierarchy will be different according to the project. So I though I will need a new table to build the HABTM, and a new class to access the parent_id to build the tree. Something like class ProjectEmployee < AR:Base belongs_to :project belongs_to :employee belongs_to :parent, :class_name => 'Employee' # <--- ?????? end class Project < AR:Base has_many :project_employee has_many :employees, :through => :project_employee end class Employee < AR:Base has_many :project_employee has_many :projects, :through => :project_employee end How can I access the parent and the childs of an employee for a given project? I need to add and remove childs as wish from the employees of a project. Thank you!

    Read the article

  • How Many HABTM relationships in cakephp is too many?

    - by user559540
    Hi all, I'm struggling with deciding how many HABTM relationships I really need in my cakephp app. I guess I don't quite get what "has" truly means. I don't want to have more HABTM tables than necessary. Here's what I have in my database: Users Properties Leases Payments Repairs user HABTM properties, user HABTM leases, user HABTM users, user HABTM repairs, property hasMany repairs, property hasMany leases, lease hasMany repairs. Also, I have two aliases for my users model (manager and tenant). This is one of the reasons I ended up with so many HABTM relationships, but I'm not sure it's necessary. Do I have too many user HABTM relationships? Would it be better to just have cake recurse through my models? Thanks!

    Read the article

  • cache_counter for habtm

    - by piemesons
    Hello How can use cache_counter in a habtm. For example a question has many tags and a tag can belong to many questions. question habtm tags Now i want to find out number of questions belonging to every tag. One way is counting everytime. But, in case of one_to_many i done same thing in this way. Like one question has many answers. then in answer model i specified belongs_to :question,:cache_counter=>true It solved my problem. So how to do the same in habtm.

    Read the article

1 2 3  | Next Page >