This is my first time using Rails and I was wondering if it's possible to load a has one polymorphic association in one SQL query? The models and associations between them are basic enough: An Asset model/table can refer to a content (either an Image, Text, or Audio) through a polymorphic association, i.e.
class Asset < ActiveRecord::...
Hi, guys!
I am trying to follow Ryan Bates screencast but have an error message. I did the following:
1) Create table
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.references :commentable, :polymorphic => true
2) Setup models
class Comment < ActiveRecord::Base
belongs_to :co...
Unluckily I can't get this relative simple stuff to work.... :-(
models:
class User < ActiveRecord::Base
belongs_to :role, :polymorphic => true
class Admin < ActiveRecord::Base
has_one :user, :as => :role
class Dealer < ActiveRecord::Base
has_one :user, :as => :role
class Buyer < ActiveRecord::Base
has_one :user, :as => :rol...
Using class table inheritance it is recommended that the subclass refers to the superclass and not the other way around. Normally in Rails polymorphic associations work in the other direction--the super refers to the subclass.
Recommended
vehicle
.id
car
.vehicle_id # PK instead of using id (identity column)
boat
.vehicle_id...
In my app I have the classes User, Video, and Vote. Users and Videos can relate to each other in two different ways: as a one-to-many or as a many-to-many. The former is when a User submits a Video (one user can submit many videos). The latter is when a user votes on a video (users have many videos through votes, and vice versa). H...
Consider:
class Person < ActiveRecord::Base
class << self
def setup
has_one :address, :as => :addressable
end
end
end
class Employee < Person
setup
end
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
# Shouldn't this be 'Employee'? Is it possible to override?
Employee.create....
Folks,
Want to make sure I understand this correctly. And please disregard the case for inheritance here (SentientBeing), trying to instead focus on polymorphic models in has_many :through relationships. That said, consider the following...
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => ...
Having set up my polymorphic relationship like so:
class Review < ActiveRecord::Base
belongs_to :reviewable, :polymorphic => true
belongs_to :user
end
class Wine < ActiveRecord::Base
has_many :reviews, :as => :reviewable
end
class Beer < ActiveRecord::Base
has_many :reviews, :as => :reviewable
end
I can do Wine.last.reviews ...
Hi,
In my code, I have 4 models: Plan, Choice, Venue, CustomSuggestion, in order to allow users to create "plans" which consist of choices, which in turn consists of a suggested.
My code looks something like this:
class Plan < ActiveRecord : Base
has_many :choices
end
class Choice < ActiveRecord : Base
belongs_to :plan
belongs_t...
If I have a model (Choice) belonging to a polymorphic association (Suggestion), is it possible to create the associated Suggestion in a form for the Choice?
Essentially, what I'm trying to do is allow a user to create a "Choice" in a poll, with potential suggestions being predefined Venues, Cities, or Districts, but also to allow an op...
Consider a class:
class Link < ActiveRecord::Base
has_many :link_votes, :as => :vote_subject, :class_name => 'Vote'
has_many :spam_votes, :as => :vote_subject, :class_name => 'Vote'
end
The problem is, when I'm adding a new vote with @link.link_votes << Vote.new the vote_subject_type is 'Link', while I wish it could be 'link_vot...
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 ...
Hi there,
I have following 'comments' table in my app:
comments
--------
id INT
foreign_id INT
model TEXT
comment_text TEXT
...
the idea of this table is to store comments for various parts of my app - it can store comments for blog post i.e:
1|34|blogpost|lorem ipsum...
user picture:
2|12|picture|lorem ipsum.....
I have a model, Report, that is polymorphic.
So many itens in my site may have many of it.
And i would like to have a generic controller for posting it.
Its a very simple model, has only a text message and the association.
in my routes, im doing something like
map.resources :users, :has_many => [ :reports ]
map.resources :posts, :has_...
class Transaction < ActiveRecord::Base
belongs_to :account, :polymorphic => true
end
class Bankaccount < ActiveRecord::Base
has_many :transactions, :as => :account
end
class Creditcard < ActiveRecord::Base
has_many :transactions, :as => :account
end
Trying to do a summation of transactions where the account is active.
Transacti...
I have articles, profiles, and comments. There is a polymorphic association between articles/profiles and comments called commentable.
On success creating a new comment I return to the commentable parent object with a sucess flash and I would like to do the same with the appropriate error flash on validation errors.
What should I pass ...
I'm designing a small database for a personal project, and one of the tables, call it table C, needs to have a foreign key to one of two tables, call them A and B, differing by entry. What's the best way to implement this?
Ideas so far:
Create the table with two nullable foreign key fields connecting to the two tables.
Possibly with...
I have a polymorphic model Comment that can be related to many types of commentables.
in my routs, for example I have:
map.resources :newsitems do |news|
news.resources :comments
end
everything is working fine, the only problem is to generate the paths.
I've in my views/controller the @commentable item, that i retrieve from a before...
What is the name for the technique of using a set of foreign keys in a table where all but one are NULL for a given row?
In other words, each row needs a foreign key to one (and only one) of n different possible tables so you actually have all the necessary foreign keys but all but one are NULL.
(users of Django may recognize this as a...
I have an invoicing system that manages debits and credits. Basically the invoice amount is obtained by the sum of its debits and the balance is derived by taking the sum of its credits and subtracting it against the total amount.
I'm doing this with four models.
Invoice
Line Item
Debit
Credit
The way it works is via a join model (L...