has-many-through

Rails: has_many through with polymorphic association - will this work?

*爱你&永不变心* 提交于 2019-11-29 19:44:14
A Person can have many Events and each Event can have one polymorphic Eventable record. How do I specify the relationship between the Person and the Eventable record? Here are the models I have: class Event < ActiveRecord::Base belongs_to :person belongs_to :eventable, :polymorphic => true end class Meal < ActiveRecord::Base has_one :event, :as => eventable end class Workout < ActiveRecord::Base has_one :event, :as => eventable end The main question concerns the Person class: class Person < ActiveRecord::Base has_many :events has_many :eventables, :through => :events # is this correct??? end

Rails Associations - has_many => :through - but same model

你离开我真会死。 提交于 2019-11-29 19:32:22
What I am trying to do: I have a blog and want to show related posts below the main post. class Post < ActiveRecord::Base has_many :related_posts has_many :posts, :through => :related_posts end And then in the join model/table class RelatedPost < ActiveRecord::Base belongs_to :post end And of course there is a table called related_posts with two post_id columns. Obviously there are several flaws with this, I'm just not sure how to make this association work in Rails. That was an interesting question. I just created a working app for your use case. post.related_posts will give you all posts

has_many :through questions

萝らか妹 提交于 2019-11-29 14:44:43
问题 I was previously using has_and_belongs_to_many, and have converted to has_many :through. Here's how it looks for a list of games that can have many users playing. With this, I can do game.users and user.games....: class Game < ActiveRecord::Base has_many :game_users, :dependent => :destroy has_many :users, :through => :game_users, :uniq => true end class User < ActiveRecord::Base has_many :game_users, :dependent => :destroy has_many :games, :through => :game_users, :uniq => true end class

How can I create new records with has_many :through and honor :conditions?

牧云@^-^@ 提交于 2019-11-29 09:42:22
问题 Let's say I have a Course in which Students can enroll via a Membership (e.g. a has_and_belongs_to_many relationsip of Courses and Students). Some memberships are for students who are just observing the class (not for credit, etc.), so: class Course < ActiveRecord::Base has_many :memberships has_many :students, :through => :memberships has_many :observers, :through => :memberships, :source => :student, :conditions => { :memberships => { :observer => true }} end Here's what works great:

How to populate fields in a has_many through join table

て烟熏妆下的殇ゞ 提交于 2019-11-29 09:24:16
问题 I have a question concerning active record association, referring to this part of the rails documentation: http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association if we have three models: class Physician < ActiveRecord::Base has_many :appointments has_many :patients, :through => :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end class Patient < ActiveRecord::Base has_many :appointments has_many :physicians,

Activeadmin formtastic dynamic select

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 02:08:40
I would like to make a dynamic select option via Activeadmin 's formtastic like so: form do |f| f.inputs "Exam Registration Details" do f.input :user_id, :as => :select, :collection => User.where(:admin => 'false') #selects user from list. WORKING f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user) #collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons. f.input :lesson_id, :as => :select, :collection => Lesson.joins(:student, :user) #collection of lessons will change to reflect lessons

has_many through additional attributes

两盒软妹~` 提交于 2019-11-29 01:51:47
How do we set additional parameters in has_many through associations? Thanks. Neelesh This blog post has the perfect solution: http://www.tweetegy.com/2011/02/setting-join-table-attribute-has_many-through-association-in-rails-activerecord/ That solution is: create your ":through model" manually, rather than through the automated way when you append to its owner's array. Using the example from that blog post. Where your models are: class Product < ActiveRecord::Base has_many :collaborators has_many :users, :through => :collaborators end class User < ActiveRecord::Base has_many :collaborators

Howto use callbacks in a has_many through association?

会有一股神秘感。 提交于 2019-11-29 01:09:43
I have a Task model associated to a Project model via has_many through and need to manipulate the data before delete/insert via the association. Since " Automatic deletion of join models is direct, no destroy callbacks are triggered. " i can not use callbacks for this. In Task i need all project_ids to calculate a value for Project after Task is saved. How can i disable delete or change delete to destroy on has_many through association? What is best practise for this problem? class Task has_many :project_tasks has_many :projects, :through => :project_tasks class ProjectTask belongs_to :project

Validate that an object has one or more associated objects

核能气质少年 提交于 2019-11-28 22:53:17
问题 I need to ensure that when a product is created it has atleast one category. I could do this with a custom validation class, but I was hoping there was a more standard way of doing it. class Product < ActiveRecord::Base has_many :product_categories has_many :categories, :through => :product_categories #must have at least 1 end class Category < ActiveRecord::Base has_many :product_categories has_many :products, :through => :product_categories end class ProductCategory < ActiveRecord::Base

How do I order a has_many through association in Ruby on Rails?

北慕城南 提交于 2019-11-28 22:16:54
Given the following AR models, I would like to sort users alphabetically by last name when given a handle to a task: #user has_many :assignments has_many :tasks, :through => :assignments #assignment belongs_to :task belongs_to :user #task has_many :assignments has_many :users, :through => :assignments I would like to get a task then navigation to its assigned users, and sort the user list alphabetically . I keep thinking that I should be able to add the :order clause to has_many :users, :through => :assignments like this: #task.rb has_many :assignments has_many :users, :through => :assignments