polymorphic-associations

How to apply different validation rule according to polymorphic association type (Rails)?

我与影子孤独终老i 提交于 2019-12-23 20:30:06
问题 I have Rails polymorphic model and I want to apply different validations according to associated class . 回答1: The class name is in the _type column for instance in the following setup: class Comment belongs_to :commentable, :polymorphic => true end class Post has_many :comments, :as => :commentable end the comment class is going to have the commentable_id and commentable_type fields. commentable_type is the class name and commentable_id is the foreign key. If you wanted to to a validation

nested form with polymorphic models

夙愿已清 提交于 2019-12-23 12:39:08
问题 I'm making an app with the following attributes, and I'm working on a creating a single form to be able to save a goal, a goal's tasks, a goal's milestones, and a milestone's tasks. #app/models/goal.rb has_many :tasks, :as => :achievement has_many :milestones accepts_nested_attributes_for :tasks accepts_nested_attributes_for :milestones #app/models/milestone.rb belongs_to :goal has_many :tasks, :as => :achievement #app/models/task.rb belongs_to :achievement, :polymorphic => true Whenever I

How does Rails populate the “model_type” field for polymorphic associations?

扶醉桌前 提交于 2019-12-23 12:09:37
问题 I have an Activity model. It belongs_to :parent, :polymorphic => true . Does Rails use parent.class.name , parent.model_name or something else to populate the parent_type field? I want a Presenter to behave like the parent object it wraps, and I need to override the right method. Thanks. 回答1: I'm working with Rails 3.0.7 right now, and the polymorphic type is being defined in active_record-3.0.7/lib/active_record/association.rb , line 1773. def create_belongs_to_reflection(association_id,

Refactoring Form_for Create Method for Comments in a Polymorphic Association

偶尔善良 提交于 2019-12-23 12:04:52
问题 I am working on my first polymorphic association relationship and I am having trouble refactoring my form_for create comments. I tried going through the Polymorphic Association RailsCasts http://railscasts.com/episodes/154-polymorphic-association?view=asciicast, but it seems dated. I have two questions: How do I rewrite my comment_form partial so that it would work for anything commentable? The way I have it right now, it would only work for traveldeals since (:commentable_id => @traveldeal

rails polymorphic association (legacy database)

只愿长相守 提交于 2019-12-23 10:06:50
问题 I am using a legacy database, so i do not have any control over the datamodel. They use a lot of polymorphic link/join-tables, like this create table person(per_ident, name, ...) create table person_links(per_ident, obj_name, obj_r_ident) create table report(rep_ident, name, ...) where obj_name is the table-name, and obj_r_ident is the identifier. So linked reports would be inserted as follows: insert into person(1, ...) insert into report(1, ...) insert into report(2, ...) insert into person

unknown attribute on polymorphic nested form object creation

断了今生、忘了曾经 提交于 2019-12-23 02:36:47
问题 I am trying to create an associated polymorphic object through fields_for. I am getting an unknown attribute error though when the parent is created, are the @order params not getting through somehow? Parent/Order controller - # GET /orders/new def new @order = Order.new @order.build_patient @order.product = OrthosisSpecification.new end # GET /hotels/1/edit def edit @order.build_patient end # POST /orders # POST /orders.json def create @order = Order.new(order_params) respond_to do |format|

How to enforce referential integrity on Single Table Inheritance?

半腔热情 提交于 2019-12-22 02:34:33
问题 I've read some of Bill Karwin's answers about single table inheritance and think this approach would be good for the setup I am considering: Playlist -------- id AUTO_INCREMENT title TeamPlaylist ------------ id REFERENCES Playlist.id teamId REFERENCES Team.id UserPlaylist ------------ id REFERENCES Playlist.id userId REFERENCES User.id PlaylistVideo ------------- id playlistId REFERENCES Playlist.id videoId REFERENCES Video.id All the CASCADE options are set to DELETE which will work

How to use constant in the ON condition in Yii2 hasMany relation

隐身守侯 提交于 2019-12-22 01:13:27
问题 I try to create a polymorphic association, what is common in Rails but unfortunately not in Yii2. As part of the implementation I need to define the relation: public function getImages() { return $this->hasMany(RecipeImage::className(), ['imageable_id' => 'id', 'imageable_type' => 'Person']); } But this doesn't work, because 'Person' is treated as an attribute of the current model, but it is a constant (class name for the polymorphic association). If I try to use 'andWhere' it adds the

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

半世苍凉 提交于 2019-12-20 21:26:08
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either

RoR: has_one “or the other”? (Or, polymorphism without the inheritance.)

拟墨画扇 提交于 2019-12-20 21:24:02
问题 Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure out is something like the following. # 1. Foo never belongs to anything. # 2. Foo MUST have one assigned sub-record for validity. # 3. Foo can only have either