FAQ Database Discussion Community


Can't figure out has_many/belongs_to RAILS 4

ruby-on-rails,ruby-on-rails-4,database-migration,has-many,belongs-to
My models are: class CarBrand < ActiveRecord::Base has_many :car_models end class CarModel < ActiveRecord::Base belongs_to :car_brand end and my migrations are class CreateCarBrands < ActiveRecord::Migration def up create_table :car_brands do |t| t.string "brand", :limit => 20 t.timestamps null: false end end def down drop_table :car_brands end end class CreateCarModels <...

Rails 4.2: has_many using condition from belongs_to

ruby-on-rails,ruby,ruby-on-rails-4,activerecord,has-many
I have three ActiveRecords. Order, which has_many OrderItems. Also has a field named status. OrderItem, which belongs_to an Order and a Script Script, which has_many OrderItems I want to set up the has_many in the Script model so that only OrderItems that belong to an Order which has a status...

Emberjs advanced sort hasMany association as a computed property

sorting,ember.js,has-many,computed-properties
I have asked a variant of this question here. But basically I need to create a computed property that operated on a hasMany association. I need to do sorting similar to the javascript sort function; where I can do something like files = ["File 5", "File 1", "File 3", "File...

ActionController::UrlGenerationError in Employees#show having has_many relation

ruby-on-rails,has-many
i am using has_many relationship where employee => has_many :insurances and insurance => belongs_to :employee The create functionality is working fine but when i am using edit, i am getting "ActionController::UrlGenerationError in Employees#show " "Showing /home/raj/Desktop/Projects/empmanagement/app/views/insurances/_show.html.haml where line #36 raised: No route matches {:action=>"edit", :controller=>"insurances", :employee_id=>#, :format=>nil, :id=>nil} missing required...

Creating relationship between 2 tables in CakePHP using models to write less code

php,cakephp,model,has-many,belongs-to
I try to say just CkMmUsersKta.user_id=CkSetupUser.id and define relationship between 2 tables in CakePhp. I defined models and called models in function in controller but says Undefined index for $c['CkSetupUser']['user_name'] CakePHP's Notice (8): Undefined index: CkSetupUser [APP\Controller\RealsController.php, line 67] my controller: class RealsController extends AppController { public $helpers = array('Html',...

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,...

No such column when creating an alias for a model association in Rails

ruby-on-rails,activerecord,associations,has-many
I want to create a one to many association between a User and Task model. In my user model I'd like to give it the alias of owner and refer to the user's tasks as owned_tasks. class User < ActiveRecord::Base has_many :owned_tasks, class_name: "Task", as: :owner end class Task <...

Rails counting rows with has_many relation

ruby-on-rails,ruby,count,has-many
I have: class Constituency < ActiveRecord::Base has_many :votes end class Vote <ActiveRecord::Base belongs_to :constituency end Now, what I want to do is to create a table, that will show me Name of constituency, number of voters (which is easy, as it is part of the table Constituency) and number of...

Get an array of many objects associations

ruby-on-rails,ruby,has-many,globalize3,globalize
I didn't know exactly how to find an understandable title so I'll try my best to explain my problem. I have 2 models: - Country translatable with globalize, with a name and many regions - Region belongs_to country What I would like to do is geting an array of all...

hasMany relation problam laravel 5

laravel,has-many
I am trying to print each page's category next to the page's name. so I did a model and named it Page. and inside I have this: public function cat() { return $this->belongsTo('App\Cat'); } this is my controller: public function pages() { $view = view('cp.pages.all'); $view->pages = Page::all(); return $view;...

CakePHP - Find hasMany association without hasMany relation in Model?

php,cakephp,has-many
Again I want ask you a question about CakePHP : I have 2 model User and Comment with relation 1-n (One User have many Comment). I want use find() to list infomation both user and it's comments with format data return : Array ( [User] => Array ( [id] =>...

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...

Relate Comments with themselves

mysql,cakephp,has-many,belongs-to
I'm new to cakePHP and the whole MVC architecture. I want to build a Reddit-like comment system where one Comment hasMany Comment(s) but also one Comment belongsTo (only) one Comment. It is the usual comment section where one comment can has a reply to that comment and the reply can...

Rails 4 and referencing parent id in 'has_many' SQL

ruby-on-rails,associations,has-many
TL;DR: How do I use the ID of the respective parent object in a has_many SQL clause to find child objects? Long version: I have the following example code: class Person < AR::Base has_many :purchases, -> { "SELECT * from purchases WHERE purchase.seller_id = #{id} OR purchase.buyer_id = #{id}" }...

Populate store in ExtJS using hasMany associations

javascript,extjs,extjs4,has-many,gridpanel
I am trying to populate all the records at once in a model and then assign each collective property to different container. i.e. I have 1 grid and 1 combo on my page and trying both in one call. So I fetch the data successfully but it comes in forms...

Rails JSON Rest API: Getting Child Class from Parent via API Call

ruby-on-rails,json,rest,has-many,belongs-to
I have two API controllers in my Rails app for a RESTful setup: StoresController (has many products) ProductsController (has one store) How can I write the API so that http://localhost:3000/api/v1/stores/37/products returns only the products for that store(in this case store #37)? I think I'm missing a route and controller method...

undefined method `model_name' for NilClass:Class when calling form in another controllers view

ruby-on-rails,has-many
I have 2 models: app/models/employee.rb: class Employee < User has_many :insurances end app/models/insurance.rb: class Insurance < ActiveRecord::Base belongs_to :employee end app/controllers/employees_controller.rb: class EmployeesController < ApplicationController before_action :set_employee, only: [:show, :edit, :update, :destroy] before_action :employee_params, only: [:create, :update] # GET /employees # GET /employees.json def index @employees = Employee.all end #...

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...

Fetch specific fields on a hasMany relation

laravel,laravel-5,has-many
I have a hasMany relation function like this: public function articles() { return $this->hasMany('App\Article'); } And use it like this: $data = \App\User::with('articles')->get(); I don't have any problems with it, since it's returning the expected data. Something like this: { "id": 1, "name": "Jhon", "lastname": "Doe", "articles": [ { "id":...

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...