FAQ Database Discussion Community


Active Record query for has_many/belongs_to associations

ruby-on-rails,activerecord,has-and-belongs-to-many
I have built a Rails app that has data about the US Congress. I have a Legislator model and a Bill model: class Legislator < ActiveRecord::Base has_many :bills end class Bill < ActiveRecord::Base belongs_to :legislator end So bills that a legislator has sponsored are tied to that legislator. The legislator...

Combining multiple relation query in one - laravel

php,mysql,laravel,laravel-4,has-and-belongs-to-many
I have 2 tables :- items and groups groups table as below :- create table groups (`id` int unsigned not null auto_increment, `group_name` varchar(255), primary key(`id`) ); items table as follows :- create table items (`id` int unsigned not null auto_increment, `group_for` int unsigned not null, `item_name` varchar(255), primary key(`id`),...

nested resources rails has and belongs to many

ruby-on-rails,has-and-belongs-to-many,nested-resources
Sorry for the generic title, but I'm not sure how to phrase it better at the moment. I finally had some time to start picking up rails again and came across this issue as I was building my models: Essentially I have a parent resource that has two nested resources....

how insert data directly and only into HABTM table ,

cakephp,e-commerce,has-and-belongs-to-many
I have 1 Category table and second Features table and have association among them is HABTM in cakephp Now i don't know exactly how to insert values in categories_features table. I used HABTM before but at that time i insert values in this table when one item is created by...

Join table comment in rails 4 migration

ruby-on-rails,ruby-on-rails-4,join,migration,has-and-belongs-to-many
I'm pretty new on rails 4 and I'm not really sure about how should be my join_table. I've done the migration rails g migration CreateJoinTableQuestionSubTopic question sub_topic And I get this file class CreateJoinTableQuestionSubTopic < ActiveRecord::Migration def change create_join_table :questions, :sub_topics do |t| # t.index [:question_id, :sub_topic_id] # t.index [:sub_topic_id,...

CakePHP select many to many (HABTM) records

php,cakephp,many-to-many,has-and-belongs-to-many
after create a many to many (HABTM) relationship between 2 classes (as code bellow) I was expecting to see results including the related records but for some reason this is not happening. Models App::uses('AppModel', 'Model'); class NewsCategory extends AppModel{ public $hasAndBelongsToMany = array('News'); } App::uses('AppModel', 'Model'); class News extends AppModel{...

Complex many-to-many relation in Rails

ruby-on-rails,activerecord,has-and-belongs-to-many
I have a model Place. For instance, place can be a city, a district, a region, or a country, but I think one model will be enough, because there is no big difference between city and region for my purposes. And places can belong to each other, e.g. a region...

How to show associated data in add form in cakephp 3.x

cakephp,has-and-belongs-to-many,cakephp-3.0
I have two tables CREATE TABLE `user_roles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `role_name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1 CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `user_role_id` int(11) DEFAULT NULL,...

Saving data with HABTM and HasMany looses foreignKey

cakephp,model,save,has-and-belongs-to-many
I have a User model with two relations: HasAndBelongsToMany public $hasAndBelongsToMany = array( 'group' => array( 'className' => 'group', 'foreignKey' => 'user_id' ) ); HasMany public $hasMany = array( 'phonenumber' => array( 'className' => 'phonenumber', 'foreignKey' => 'user_id' ) ); Phonenumber and Group have set public $belongsTo = array( 'User'...

Two or more Many to Many relationships between two tables in rails

ruby-on-rails,activerecord,has-and-belongs-to-many
I have two tables: Users and Groups a User has_many groups and a group, has_many a users: u = User.last u.groups g = Group.last g.users Supposed I wanted a second list of different groups, for some strange reason. Where once again a User has may groups (called other_group in this...

Return JSON array of associated entity IDs in CakePHP 3.0 belongsToMany relationship

cakephp,has-and-belongs-to-many,cakephp-3.0
If I have two tables, say foos and bars, in a many-to-many relationship (via a bars_foos join table, in this case), what is the best way in CakePHP 3.0 to include an array of the IDs of Bars associated with each Foo in the JSON returned by the RequestHandler through...

Selecting Third-tier ActiveRecord_Relation - Rails, ActiveRecord, HABTM

ruby-on-rails,ruby,database,activerecord,has-and-belongs-to-many
I have three models. User, Message and Filter, with the relations shown below. How can I select User.find(*).filters.messages? User: has_many :filters has_and_belongs_to_many :messages Message: has_and_belongs_to_many :users has_and_belongs_to_many :filters Filter: belongs_to :user has_and_belongs_to_many :messages EDIT 1: I am assigning the result to an instance variable called @filtered_messages and want to iterate...

Translating query involving join table from CakePHP 1.3 to 3

has-and-belongs-to-many,cakephp-3.0
I'm migrating an application from Cake 1.3 to 3.0. My database has teams and people (with teams_people to connect them via belongsToMany relations) and divisions (teams belongTo divisions). The teams_people table has additional information, like whether the person's role on the team (captain, player), jersey number, position, that sort of...

Has many through stop printing the entire object after loop

ruby-on-rails,ruby-on-rails-4,many-to-many,views,has-and-belongs-to-many
I am practicing has_many through but I am stuck when the looping through the contents. It displays the whole object and not just the item want. Models: class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Physician < ActiveRecord::Base has_many :appointments has_many :patients, through: :appointments end class Patient <...

HABTM CakePHP no results for related model

cakephp,cakephp-2.0,has-and-belongs-to-many
I have problem with HABTM models. When I try to fetch any related model f.e. like this: $this->Tagi->find('first'); I dont get any results for associated model. Result looks like this: array( 'Tagi' => array( 'id' => '1', 'nazwa' => 'sth' ), 'Instytucje' => array() ) I am sure that there...

Laravel Eloquent: Get record that contains two specific relations

laravel,eloquent,laravel-5,has-and-belongs-to-many
I have a project where a user can create conversations with other users. A conversation can belongsToMany users and user can belongsToMany conversations. I now need to get the conversation in which two specific users participate. I tried a combination of solutions using whereIn and I tried the following: $c...

Rails: uninitialized constant Model::RenamedAssociation when using class to specify model in relationship

ruby-on-rails,model,associations,has-and-belongs-to-many
I have 3 tables, Charges, Transactions and Charges_Transactions. I had to rename the associations on my models as there is an existing transactions method which was interfering with the association. See this article class Charge < ActiveRecord::Base has_and_belongs_to_many :payment_transactions, join_table: "charges_transactions", foreign_key: "charge_id", association_foreign_key: "transaction_id", class: 'Transaction' end class Transaction...

Rails, using has_and_belongs_to_many with abstract Model

ruby-on-rails,ruby,associations,has-and-belongs-to-many
I have abstract model Detail that has child models like Carcass, Basis. Also i have User model. Can I use has_and_belongs_to_many association between User and Detail? Could it be inherited to child models? Or do I need to use that association between User and each model of Detal? The main...

CakePHP 3 belongsToMany Validation

validation,cakephp,model,has-and-belongs-to-many,cakephp-3.0
I am struggling with how to do validation with belongsToMany relationships. Namely, the classic recipes/ingredients relationship. I would like a recipe to always have an ingredient on create or edit. What would my validation look like in my RecipesTable? I have tried: $validator->requirePresence('ingredients')->notEmpty('ingredients') As well as $validator->requirePresence('ingredients._ids')->notEmpty('ingredients._ids') The second one...

Rails HABTM: Select everything a that a record 'has'

ruby-on-rails,postgresql,join,has-and-belongs-to-many
I have three tables: projects, technologies, and projects_technologies (a map from project IDs to/from technology IDs). I have two models: projects and technologies. Projects have and belong to many (HABTM) technologies and vice versa. Now I'm trying to get all the names (or active records, if there's a way to...

rails 4 HABTM relation and extra fields on join table

ruby-on-rails,join,entity-relationship,has-and-belongs-to-many
What I have (pseudo code): model Document column :title HABTM :users model User column :name HABTM :documents Document has users (being approvers for document, either approve or not), and in this context join table should have extra column approved for each user. jointable user_id, document_id, approved 1 , 1 ,...