FAQ Database Discussion Community


Updating an association between existing records

ruby-on-rails,has-many-through,model-associations
I have a users table and services table: class User < ActiveRecord::Base has_many :services, through: :services_users end class Service < ActiveRecord::Base has_many :users, through: :services_users end with the join table: create_table "services_users", id: false, force: :cascade do |t| t.integer "service_id", limit: 4 t.integer "user_id", limit: 4 end I have a...

Rails - build associated record with additional params in join table

ruby-on-rails,ruby-on-rails-4,has-many-through
I have has_many_through relationship: class Schedule < ActiveRecord::Base has_many :schedule_tasks has_many :tasks, :through => :schedule_tasks class Task < ActiveRecord::Base has_many :schedule_tasks has_many :schedules, :through => :schedule_tasks When I create the record, the join table (schedule_tasks) record that's created only populates the bare minimum fields to make the join, in this...

Has Many Through two models

ruby-on-rails,has-many-through
I have two models of importance here. There's more models interacting with them too, but only these should be relevant. class Image < ActiveRecord::Base belongs_to :user belongs_to :trip end class Mission < ActiveRecord::Base belongs_to :user belongs_to :trip # has_many :images, through: :user end The commented out line gets me halfway...

Add field for an has_many association with :through

sql,ruby-on-rails,model,has-many-through,has-many
I have a question about has_many associations : Here is a sample of my db/models : Table document id : int name : text class Document < ActiveRecord::Base has_many :document_pages has_many :pages, through: :document_pages end Table page id : int content : text class Page < ActiveRecord::Base has_many :document_pages has_many...

Creating associated has_many :through records with nested forms

ruby-on-rails,nested-forms,has-many-through
Here are the models in my many-to-many relationship: class Student < ActiveRecord::Base has_many :classroom_memberships, :dependent => :destroy has_many :classrooms, :through => :classroom_memberships end class ClassroomMembership < ActiveRecord::Base belongs_to :classroom belongs_to :student end class Classroom < ActiveRecord::Base has_many :classroom_memberships has_many :students, :through => :classroom_memberships end When creating a new student, I...

Ruby on Rails: adding association based on user input, many to many using :through, create and destroy method errors

ruby-on-rails,ruby,many-to-many,has-many-through
I'm trying to build a simple project management app where a user can be a member of many projects and a project can be a member of many users. This many to many relation is represented in my models below: #project model class Project < ActiveRecord::Base has_many :memberships, dependent: :destroy...

How do I write this has_many_to_many dependency?

ruby-on-rails,ruby,ruby-on-rails-4,has-many-through
I am trying to create a has many to many relationship between a Goal model. A goal can have dependent goals and a goal can have others goals that rely on it. So far I have come up with the below, but it does not seem to be working. class...

Form including check_box_tag — params not updating a has-many-through relationship

ruby-on-rails,checkbox,parameters,has-many-through,form-for
I'm trying to implement a check_box_tag to stock a Fridge model object with Ingredients; the two models are linked via a has-many-through relationship (FridgeIngredient model). It's worth noting that each Fridge also belongs to a User, which I worry might be causing the issue here in some manner (as this...

Has Many Through relationship with accepts nested attributes

ruby-on-rails,associations,nested-attributes,has-many-through
I understand that in order to use nested attributes I need to use a has many through relationship instead of a has and belong to many so I have the following set up: class Deed < ActiveRecord::Base has_many :deeds_title_abstracts has_many :title_abstracts, through: :deeds_title_abstracts end class TitleAbstract < ActiveRecord::Base has_many :deeds_title_abstracts...

Laravel Eloquent hasManyThrough Relationship, SELECT without

laravel-4,eloquent,has-many-through
I have 3 entities: Company, User, Ticket I need to select ALL companies that have no tickets, how can I manage that using Eloquent? this are the relationships class Company extends Eloquent { protected $table = 'companies'; protected $fillable = array('name', 'priority', 'shortname', 'color'); public function users() { return $this->hasMany('User',...

Scope ActiveRecord Relation based on record data

ruby-on-rails,ruby-on-rails-4,activerecord,has-many-through,has-many
I a model like this: class Club < ActiveRecord::Base belongs_to :organization has_many :career_clubs has_many :careers, through: :career_clubs end Which appropriately gets careers through the table career_clubs. So far this is only using the club's id field and getting all the records in the career_clubs table with that id. What I...

Cannot access associated model attributes within a form

ruby-on-rails,ruby,forms,model,has-many-through
I am trying to build a form for a has_many :through relationship. The Problem is, that I can access the attributes from the join table (:through) in the form, but not the other table. My models look like this: class Recipe < ActiveRecord::Base has_many :quantities has_many :ingredients, through: :quantities accepts_nested_attributes_for...

has many through - get related object even if no associations exist

ruby-on-rails,has-many-through
I've got a has_many :through relationship set up: class Dish has_many :dish_allergenes has_many :allergenes, :through => :dish_allergenes end class DishAllergene belongs_to :dish belongs_to :allergene end class Allergene has_many :dish_allergenes has_many :dishes, :through => :dish_allergenes end There is a basic set of about 10 allergenes. When adding or editing a dish,...

Rails - If / Else in view returning both branches

ruby-on-rails,if-statement,model-view-controller,conditional-statements,has-many-through
I've an 'if / else' running in my view, based on the current URL of the page, and currently the view is displaying what is should were the 'if' both true and false. It's a little tricky to explain, and I've no idea why this is happening - any explanations...

Rails 4 many to many joint table

has-many-through,ruby-on-rails-4.2
I'm trying to learn many to many relationship, so I have two model Order and Product, I generated with scaffold a join table orders_products with the follow migration: create_table :orders_products do |t| t.references :order t.references :product end I have in order model: has_many :orders_products has_many :products, through: :orders_products accepts_nested_attributes_for :orders_products...

Creating a feed => NoMethodError: undefined method for ActiveRecord_Associations_CollectionProxy

ruby-on-rails,rails-activerecord,has-many-through,model-associations,rails-postgresql
I am implementing a feed of all cards belonging to the tags a user has subscribed to and I get the following error. It's probably something trivial but I can't pinpoint what needs work. NoMethodError: undefined method `cards' Tag::ActiveRecord_Associations_CollectionProxy:0x007fbaa46239f8> Here are my models: class User < ActiveRecord::Base has_many :cards, dependent:...

Rails class comparison - If Class.children include AnotherClass.children do

ruby-on-rails,ruby,arrays,has-many-through,comparison-operators
Simple question I hope. I've a couple of classes - User and Recipe - both of which have 'ingredients' as children via has-many-through relationships. I'd like to run a comparison that checks to see if User.ingredients include the ingredients for each Recipe. I thought a simple 'include?' query would work...

Has many association on tagging user in a post

ruby-on-rails,ruby,ruby-on-rails-4,has-many-through,polymorphic-associations
In my app a User can Post something to his timeline and Tag other's users in his post (something like facebook). In my model I have to know who was tagged in which post and who did tag (always the post's owner). I've found something similar here with hashtags model,...

Laravel hasManyThrough issue with same named field of tables

laravel,laravel-4,orm,eloquent,has-many-through
These are the models I have : class User extends Eloquent { public function categories() { return $this->hasMany('Category'); } public function forms() { return $this->hasManyThrough('App\Models\Form', 'Category'); } } class Category extends \Eloquent { public function user() { return $this->belongsTo('User'); } public function forms() { return $this->hasMany('App\Models\Form'); } } namespace App\Models;...

Rails 3 : Cannot update join table records with has_many through

ruby,ruby-on-rails-3,join,has-many-through,has-many
Setup class ContactGroup < ActiveRecord::Base has_many :accounts_contact_groups has_many :accounts, through: :accounts_contact_groups end class AccountsContactGroup < ActiveRecord::Base # Join table belongs_to :account belongs_to :contact_group end class Account # nothing linking to above # as I do not need a relationship from this direction end Data > ContactGroup.all => [#<ContactGroup id: 7,...

'where' condition in has_many_through in kohana

php,many-to-many,kohana,has-many-through
I have two models Illness and Symptom: Illness class Model_Illness extends ORM { //protected $_db_group = 'default2'; protected $_table_name = 'illnesses'; protected $_has_many = array( 'symptoms' => array( 'through' => 'symptoms_illnesses', 'foreign_key' => 'illness_id', ) ); Symptom class Model_Symptom extends ORM { //protected $_db_group = 'default2'; protected $_table_name = 'symptoms';...

undefined method `first' for nil:NilClass Ruby

ruby-on-rails,has-many-through
I'm confused about this error. I'm getting it without any "firsts" anywhere . . . The line that calls the error is the following: = @wiki.creator.name My wikis have many users, and my users have many wikis, through a table called Relationships (with attributes :wiki_id, :user_id, :creator_created). Creator is a...

Rails - Many to Many association (Adding User to Groups) SQLite

ruby-on-rails,ruby,many-to-many,has-many-through
I have a Many-to-Many relationship here. class Project < ActiveRecord::Base has_many :memberships has_many :users, :through => :memberships end class Membership < ActiveRecord::Base belongs_to :project belongs_to :user end class User < ActiveRecord::Base has_many :memberships has_many :projects, :through => :memberships end These are the three models that I have. I want to...

NoMethodError: undefined method `user' for has_many :through Association

ruby-on-rails,ruby,database,has-many-through,nomethoderror
I've attempted to create a has_many :through Association in my rails application, however I get an error when I do the following in the rails console. 2.0.0-p481 :001 > @group = Group.first Group Load (0.2ms) SELECT "groups".* FROM "groups" ORDER BY "groups"."id" ASC LIMIT 1 => #<Group id: 1, name:...

Rails and Globalize - join translation table of related model in scope

ruby-on-rails,ruby-on-rails-4,relationship,has-many-through,globalize
I want to order some records of a model that has a relation to another model (with translated attributes). Here an example: I have a model Project I have a model Task I have the relation Project has_many Tasks The model Task has attribute name globalized (on task_translations table) Now,...

many to many association mass assign checkboxes ruby on rails 4

ruby-on-rails-4,checkbox,many-to-many,has-many-through,jointable
what i want to accomplish it's almost exactly the same as this! railscast tutorial but can't get it to work.I have a courses table and a enrollments table they have a many to many relation so i have a join table called "courses_enrollments" i'd like to be able to add...

ActiveRecord::AssociationTypeMismatch: for has_many :through relationship

ruby-on-rails,ruby,activerecord,has-many-through,has-many
I am modeling a game which has many players. Players can also play multiple games. I have users, games and played_game which is acting as the join table. This is my first foray into has_many :through relationships so I'm hopeful that this is a simple question. Or at least a...

implement has_many through association in rails 4

ruby-on-rails-4,activerecord,has-many-through
I am working with has_many :through association in Application. I implement association between models like :- in employee.rb class Employee < ActiveRecord::Base has_many :inventories, through: :employee_inventories end in inventory.rb class Inventory < ActiveRecord::Base has_many :employees, through: :employee_inventories end in employee_inventories.rb class EmployeeInventory < ActiveRecord::Base belongs_to :employee belongs_to :inventory end I...