belongs-to

adding items belongs_to relationship to Active Admin

◇◆丶佛笑我妖孽 提交于 2019-12-01 13:10:39
I'm using active admin for my rails app. I have a customer model which belongs_to a department and also belongs_to a delivery_time. In my admin folder I have a customer.rb file for active admin. That file looks like this - ActiveAdmin.register Customer index do |customer| column :department, :sortable => false column :delivery_time, :sortable => false end end Essentially, I'm trying to customise the customer section of active admin to show the name of department they belong to and what delivery time they belong to. The department model has a name and a some other properties - the name of the

find all parent records where all child records have a given value (but not just some child records)

断了今生、忘了曾经 提交于 2019-11-30 16:57:51
An event has many participants. A participant has a field of "status". class Event < ActiveRecord::Base has_many :participants end class Participant < ActiveRecord::Base belongs_to :event end I need to find all events except the following ones: events where every one of its participants has a status of 'present'. I can find all events where some of its participants have a status of 'present' with the following AR code: Event.joins(:participants).where .not(participants: {status: 'present'}) .select("events.id, count(*)") .group("participants.event_id") .having("count(*) > 0") That creates SQL

find all parent records where all child records have a given value (but not just some child records)

跟風遠走 提交于 2019-11-30 16:18:48
问题 An event has many participants. A participant has a field of "status". class Event < ActiveRecord::Base has_many :participants end class Participant < ActiveRecord::Base belongs_to :event end I need to find all events except the following ones: events where every one of its participants has a status of 'present'. I can find all events where some of its participants have a status of 'present' with the following AR code: Event.joins(:participants).where .not(participants: {status: 'present'})

Nested attributes for belongs_to association rails

久未见 提交于 2019-11-30 11:45:39
I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints. # Models class Complaint < ActiveRecord::Base attr_accessible :complaint, :date, :resolved belongs_to :user, :class_name => 'User', :foreign_key => 'id' belongs_to :company, :class_name => 'Company', :foreign_key => 'id' has_many :replies accepts_nested_attributes_for :company end class Company < ActiveRecord::Base attr_accessible :name has_many :complaints, :class_name => 'Complaint', :foreign_key => 'id' has_many :branches, :class_name => 'Branch',

Validation failed Class must exist

馋奶兔 提交于 2019-11-30 04:44:06
I have been (hours) trouble with associations in Rails. I found a lot of similar problems, but I couldn't apply for my case: City's class: class City < ApplicationRecord has_many :users end User's class: class User < ApplicationRecord belongs_to :city validates :name, presence: true, length: { maximum: 80 } validates :city_id, presence: true end Users Controller: def create Rails.logger.debug user_params.inspect @user = User.new(user_params) if @user.save! flash[:success] = "Works!" redirect_to '/index' else render 'new' end end def user_params params.require(:user).permit(:name, :citys_id)

Rails: Non id foreign key lookup ActiveRecord

旧街凉风 提交于 2019-11-30 03:02:51
I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes , this rightly triggers SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2) (2 being the that coach's id here which is my problem.) I want it to trigger SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David') ('David' being the that coach's user_name ) user_name is

Nested attributes for belongs_to association rails

蓝咒 提交于 2019-11-29 17:44:39
问题 I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints. # Models class Complaint < ActiveRecord::Base attr_accessible :complaint, :date, :resolved belongs_to :user, :class_name => 'User', :foreign_key => 'id' belongs_to :company, :class_name => 'Company', :foreign_key => 'id' has_many :replies accepts_nested_attributes_for :company end class Company < ActiveRecord::Base attr_accessible :name has_many

Laravel Relationships

元气小坏坏 提交于 2019-11-29 06:07:38
I've been looking over relationships in Laravel 4 in the documentation and I'm trying to work out the following. I have a table in my database called 'events'. This table has various fields that mainly contain ID's that relate to other tables. For example, I have a 'courses' table. The events table contains a field called 'course_id' which relates to the ID of the 'id' field in the courses table. So basically, I'm after some advice on how you go about relating the two (belongsTo()?) and then passing the connected data to the view. Here is where I am at so far http://paste.laravel.com/pf3 . I

Rails: Non id foreign key lookup ActiveRecord

对着背影说爱祢 提交于 2019-11-29 00:39:43
问题 I want ActiveRecord to lookup by a non-id column from a table. Hope this is clear when I give you my code sample. class CoachClass < ActiveRecord::Base belongs_to :coach end class Coach < ActiveRecord::Base has_many :coach_classes, :foreign_key => 'user_name' end When I do a coach_obj.coach_classes , this rightly triggers SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2) (2 being the that coach's id here which is my problem.) I want it to trigger SELECT * FROM `coach_classes

Rails belongs_to many models [closed]

我的未来我决定 提交于 2019-11-28 16:38:31
I did find some questions on SO about Rails associations that are somewhat like my question, but for the life of me I can't seem to understand how to use belongs_to multiple models. Here's the table structure I intend to have: User id Post id user_id #foreign key; a post belongs to a User aka "Who created this post" Comment id user_id #foreign key; a comment belongs to a User aka "Who made this comment" post_id #foreign key; a comment belongs to a Post aka "What post this comment is for" And the associations : User has_many :posts has_many :comments Post belongs_to :user has_many :comments