has-many

Rails Filter records of child model based upon the parent model attribute

你离开我真会死。 提交于 2020-01-01 19:47:27
问题 Following are the 1-to-M models: class FotoGossip < ActiveRecord::Base has_many :uploads attr_accessible :published_at, ... end class Upload < ActiveRecord::Base belongs_to :foto_gossip end Now I want the Uploads.all with the condition :published_at NOT NULL of the corresponding upload's parent model? 回答1: Just add this to your Upload model: named_scope :with_published_foto_gossip, :joins => :foto_gossip, :conditions => "foto_gossips.published_at IS NOT NULL" then you can get all the uploads

What's the rails way to load other models collections for new, edit update and create actions?

前提是你 提交于 2019-12-31 05:02:07
问题 How is the best way to load Category model, for ProductController in new, edit update and create actions Product has categories collection class Product < ActiveRecord::Base has_many :categories end Always for new, edit, create and update actions, I need to load the categories collection to populate a check_box_tag list The "baby steps" for this situation is: class Admin::ProductsController < Admin::AdminApplicationController def new @product = Product.new @categories = Category.all end def

rails map.resources with has_many :through doesn't work?

冷暖自知 提交于 2019-12-30 05:14:06
问题 I've got three (relevant) models, specified like this: class User < ActiveRecord::Base has_many :posts has_many :comments has_many :comments_received, :through => :posts, :source => :comments end class Post < ActiveRecord::Base belongs_to :user has_many :comments end class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end I'd like to be able to reference all the comments_received for a user with a route - let's say it's for batch approval of comments on all posts. (note that

Rails Model has_many with multiple foreign_keys

自闭症网瘾萝莉.ら 提交于 2019-12-27 17:05:50
问题 Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a has_many (the first gets overwritten). class Person < ActiveRecord::Base belongs_to :father, :class_name => 'Person' belongs_to :mother, :class_name => 'Person' has_many :children, :class_name => 'Person', :foreign_key => 'mother_id' has_many

Rails Model has_many with multiple foreign_keys

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-27 17:04:27
问题 Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, but obviously I can't repeat the :children in a has_many (the first gets overwritten). class Person < ActiveRecord::Base belongs_to :father, :class_name => 'Person' belongs_to :mother, :class_name => 'Person' has_many :children, :class_name => 'Person', :foreign_key => 'mother_id' has_many

Creating a nested model instance (has_many relation) on the view of its parent model

爷,独闯天下 提交于 2019-12-25 12:48:25
问题 I have a model A und a model B and the relation is A has_many B (and B belongs_to A). I have a model, a controller and views for A and only a model for B. I want to create and edit the instances of B on the edit view of A ( url/a/1/edit). I know I can create a controller for B and call those methods using a form in the view of A, but then I need to redirect back to the view of A, because I don't want actual views for B. Is there a recommended way to do this? What I want is to not break any of

Ember 2.7, Rails 5, JSONAPI, Active Model Serializers - counting the number of records in a hasMany relationship

感情迁移 提交于 2019-12-25 04:48:12
问题 This idea started with this question. 2 models: class Trail < ApplicationRecord has_many :notes, dependent: :destroy end class Note < ApplicationRecord belongs_to :trail end 2 serializers: class NotesSerializer < ActiveModel::Serializer attributes :id, :note_body belongs_to :trail end class TrailSerializer < ActiveModel::Serializer attributes :id, :name, :notes_count has_many :notes def notes_count object.notes.size end end When calling the route trails/index, this will fire for every Trail,

Querying Relationship Existence, how to add conditions depending on an array length

江枫思渺然 提交于 2019-12-25 03:15:42
问题 I would like to filter an item (pubs, in this case) by some characteristics that are stored in a separate table (tapps, in this case), and both are related by pub_tapps. I have the following tables: pubs, tapps, pub_tapps(pub_id, tapp_id) The relation between Pub and Tapp is the following: public function pubTapps() { return $this->belongsToMany(Tapp::class, 'pub_tapps'); } in my Pub model I tried the following testing for an array $request=[5,8, 7]: public function pubsFilteredByTapps

Rails 4 Relation is not being sorted: 'has_many' association w/ default scope, 'includes' and 'order' chained together

跟風遠走 提交于 2019-12-24 14:46:45
问题 I am trying to use a default scope to impose a sort order on the model QuizCategoryWeight. The goal is to get @possible_answer.quiz_category_weights to return the weights in sorted order. Update: I have narrowed the problem down to the fact that default scopes seem to work for me as long as they just have an 'order' method but not when the 'includes' method is chained with the 'order' method. However, this chaining does work for named scopes. Could it be my development environment? Or is this

rails_admin Change belongs_to Drop-down to Display Options from Different Field

点点圈 提交于 2019-12-24 02:07:39
问题 I am using rails_admin 0.6.5 with Rails 4.1.6 and have a has_many / belongs_to association setup between the Volume and Issue models respectively: class Volume < ActiveRecord::Base has_many :issues, :inverse_of => :volume validates :number, presence: true, numericality: {greater_than_or_equal_to: 1} end and the Issue model: class Issue < ActiveRecord::Base belongs_to :volume, :inverse_of => :issues validates :number, presence: true, numericality: {greater_than_or_equal_to: 1} validates :date,