has-and-belongs-to-many

How can I join the same 2 models twice in Rails?

孤人 提交于 2019-12-13 16:08:36
问题 I have an app with country preferences. A user will have 2 types of country preferences - event and research. In the future there may be more. I was leaning more towards having 2 tables to represent this over using STI. I'm having a bit of trouble configuring Rails elegantly to do this. I could hack it but I would rather do this by Rails convention. What I want is something like this: class User < ActiveRecord::Base has_many event_countries, :through => :event_countries, :class_name =>

Rails Query to return users belongs to any cities & not belong to any cities

若如初见. 提交于 2019-12-13 15:55:57
问题 I have Many to Many Associations between two tables: For Ex users & cities users id name 1 Bob 2 Jon 3 Tom 4 Gary 5 Hary cities id name 1 London 2 New-york 3 Delhi users_cities id user_id city_id 1 1 2 2 2 1 3 3 1 4 3 2 5 4 3 I want two sql queries Query which accepts array of city_id and return all the users belongs to that cities. For Ex when city_id : [1, 2] then result should be O/P should be id name 1 Bob 2 Jon 3 Tom Query which accepts array of city_id and return all the users who do

HABTM form validation with CakePHP 2.x

五迷三道 提交于 2019-12-13 11:14:32
问题 I have a HABTM relation like : Post <-> Tag (a Post can have multiple Tag, and same the other way). This work with the multiple checkbox selection generated by Cakephp. But I want to have at least one Tag for every Post and throw an error if someone try to insert an orphan. I'm looking for the cleanest/most CakePHP alike way to do this. This is more or less an update of this HABTM form validation in CakePHP question, as I get the same problem on my cakephp 2.7 (last cakephp 2.x for now with

Complex forms - Managing multiple models in a single form - (3rd degree madness)

China☆狼群 提交于 2019-12-13 05:22:04
问题 I have isolated the problem in the second block of code below (if you don't want all the details): I can create new users from the account model. I can't assign those users roles from the accounts model. I am using fields_for, this method does not work when I attempt to assign the role_ids to the roles model. My db is set up in the following way: Account model has_many : users User model has_and_belongs_to_many : roles , belongs_to : accounts Roles model has_and_belongs_to_many : users views

How to use :inverse_of with multiple associations?

冷暖自知 提交于 2019-12-13 00:08:38
问题 Does anyone know what I should put in the empty fields below to make this relationship work out of the box? I am very close to making this work as all associations work flawlessly. The only issue is that I cannot save a user with fruits attached to it as, currently, there is no :inverse_of. I need the :inverse_of pointing in the right direction so that I can save a user with fruits instead of having to save the user first and then attach fruits to it later. Thank you! UPDATED AFTER COMMENTS:

ActiveRecord fails to update HABTM relation

家住魔仙堡 提交于 2019-12-12 19:32:12
问题 I'm using a simple model for user authorisation with two ActiveRecords User and Role User and Role have a HABTM relation to each other. I tried to created a user interface for assigning roles to users with simple checkboxes - just like in Railscasts Episode #17. My problem is that neither User#new nor User#update_attributes use the parameters submitted by my form to update the relation between the User object and its roles. params[:user][:role_ids] contains the correct values. But calling

CakePHP multiple model conditions in find() with HABTM

可紊 提交于 2019-12-12 09:04:44
问题 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(

cakephp habtm join issue

房东的猫 提交于 2019-12-12 06:05:49
问题 I have table structure like below: events (boxing, sparring etc) competitors (users who are participating into diff events) events_competitors (every competitor's selected events will go here) Now what i want is, as per my match schedules i want to find competitors from schedules tables, where each competitors will be matching with events they have selected at registration time. I'm using a find query something like this: $matchdivisions = $this->Competitor->find("all" , array( 'conditions' =

A related Model isn't being validated

廉价感情. 提交于 2019-12-12 04:15:43
问题 I currently have the following models: class Category extends AppModel { var $name = 'Category'; /*var $validate = array( 'name' => 'multiple' ); no idea how to use this */ var $hasAndBelongsToMany = array( 'Post' => array( 'className' => 'Post' ) ); class Post extends AppModel { var $name = 'Post'; var $hasAndBelongsToMany = array( 'Category' => array( 'className' => 'Category' ) ); var $belongsTo = array( 'Page' => array( 'className' => 'Page' ) ); class Page extends AppModel { var $name =

Retrieving data through two model relationships

▼魔方 西西 提交于 2019-12-12 03:56:09
问题 I'm trying to retrieve some data through two model relationships with CakePHP. The models and their associations are as follows: User hasOne Profile HABTM Skill I would like the user's skills to be returned when I do a find() operation on the User model, and right now it isn't returned. Here's my find call which is being executed against the User model: $this->paginate = array( 'conditions' => array( 'OR' => array( 'Profile.firstname LIKE' => "%$q%", 'Profile.lastname LIKE' => "%$q%" ) ) );