Displaying 20 results from an estimated 520 matches for "has_one".
2008 Sep 16
3
has_one :through eager-loading problem
I have a problem with the :include option in find for my
has_one :through association.
I have Users, Profiles and Customers, all connected to each other with
a has_one :through => :membership association like this:
class Profile < ActiveRecord::Base #could also be user or customer
has_one :membership # i saw an acrticle of Ryan about
has_one :through,...
2006 May 12
1
can I fake has_one :through?
...they map into Rails. I''ll try
to lay out my problem using the has_many and belongs_to structures. I
have a RuleSpace, a Rule, and a Subject.
A RuleSpace has_many Rules
A RulesSpace has_many Subjects through Rules
A Rule belongs_to (many) RuleSpaces
A Rule has_many Subjects
A Subject has_one Rule
A Subject has_one RuleSpace
So given a single Subject I should be able to find a single Rule and
a single RuleSpace. Going the other way, given a RuleSpace I should
find many Subjects.
I''m thinking the model definitions should look like this:
class RuleSpace < AR::Base
ha...
2006 Mar 23
4
belongs_to more than one model
...And I have two other tables that contains states:
houses
id
color
state_id
places
id
place_name
state_id
How would my model relationships look like?
class State < ActiveRecord::Base
belongs_to house
belongs_to place
end
class House < ActiveRecord::Base
has_one state
end
class Place < ActiveRecord::Base
has_one state
end
Is that right? Can I have a model that belongs to multiple models?
--
Vincent H Gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/200603...
2006 Apr 02
7
RANT: belongs_to -> refers_to
...f thumbnails (stored with file_column) and Textile fields.
I start out with
class Item < ActiveRecord::Base
:has_many :pictures
end class
But I want one of those pictures to be featured as the gallery photo. So,
naturally, I gravitate towards adding
class Item < ActiveRecord::Base
:has_one primary_picture, :class_name => ''Picture''
end
I am encouraged in this futile pursuit by the docs, which I quickly skim to
see things like
:has_one :last_comment, :class_name => "Comment", :order => "posted_on"
Of course, in the light of day, it...
2008 Sep 29
1
has_one :through with :source. How to alias association?
...validates_presence_of :name, :on => :create, :message => "can''t be
blank"
validates_uniqueness_of :name, :message => ''name must be unique''
validates_presence_of :description, :on => :create, :message => "can''t
be blank"
has_one :attachment, :as => :attachable, :dependent => :destroy
has_one :logo, :through => :attachment, :source => :attachable,
:source_type => ''Maker'', :dependent => :destroy,
:conditions => [''attachments.content_type = ?'',
&...
2010 Nov 28
6
has_one accepts_nested_attributes_for fields_for NOT WORKING HELP
MODEL
class User < ActiveRecord::Base
has_one :address, :dependent => :destroy
accepts_nested_attributes_for :address
end
CONTROL
def new
@user = User.new
@user.build_address # Adicionei
...
VIEW partial _form
....
<% f.fields_for :address do |b| %> # Adicionei
<%= b.text_field :city_manual %> # Adicion...
2005 Dec 15
8
How to ensure deletes cascade to associated models?
I have the following models:
class Resume
belongs_to :resume_assignments
end
class User
belongs_to :resume_assignments
end
class ResumeAssignment
has_one :resume
has_one :user
end
When I delete a resume, I want to make sure that its associated
ResumeAssignment gets deleted also. Calling destroy_all to delete
some resumes doesn''t seem to do this. Is there an option that will
make it do this?
Thanks,
Carl
2006 Apr 20
4
Question about Associations
...Got a stupid-simple question about associations. I have two
models - school and course. There are a fixed number of schools (set
up in the migration). Each course is assigned a school and a school
will be associated with multiple courses...
How do I set up the associations? Do (can) I have School :has_one
:course and Course :has_many :schools?
Does the schools table then get a course_id field or the other way around?
I don''t think I should have course :belongs_to School, as there will
only be one instance of a school in the Schools table.
Thanks!
jt
2010 Aug 10
4
(Dreaded) STI, belongs_to
...w process, represented by a state machine (this is not a
state machine question). So there are two state machine classes that
differ slightly and subclass a generic StateMachine:
class ScholarshipApplication
belongs_to :state_machine
end
class StateMachine
end
class StateA < StateMachine
has_one :application
end
class StateB < StateMachine
has_one :application
end
The problem is I can get a ScholarshipApplication instance to tell me
its StateMachine, but the StateMachine can''t tell me it''s
ScholarshipApplication. E.g.:
app = ScholarshipApplication.create
state =...
2006 Apr 07
2
has_one not using the pk?
...andle
normalized informations which aren''t connected via the pk?
create table what (
myid int,
myclass int, ;; references blahclass.blahid
mygender int
)
create table blahclass (
blahid int,
mdesc1 text,
fdesc1 text)
)
I would like to wrap this as
class What < AR::Base
has_one :blahclass
class Blahclass < AR::Base
belongs_to :who
The problem is that a lookup of who.blahclass uses the who.pk to get the
Blahclass record, but it should use who.blahclass. I dont find a way to
specify this.
Of course it would be easy to hardwire the logic into a method, but i
would l...
2008 Feb 19
0
Issue with Observers and has_one association...
Hey Guys,
I''m getting a peculiar bug working with Observers and has_one
associations. When a has_one association is invoked by an Observer it
always returns null. I looked in the development.log file and the SQL
for the has_one association isn''t even being executed. On the other
hand, when I replace the has_one association with just a hand crafted
method, it...
2006 Jul 30
3
ActiveRecord - Multiple Address for a single record
...alse
t.column :country, :string
t.column :addressable_id, :integer
t.column :addressable_type, :string
This would appear to give me the basic polymorphic assocation. Here is
my challange. Mapping to location seems simple:
class Location < ActiveRecord::Base
has_one :address, :as => :addressable
end
What I''m having trouble with is the company model. I''ve currently got:
class Company < ActiveRecord::Base
has_one :main_address, :as => :addressable
has_one :billing_address, :as => :addressable
has_...
2006 Feb 17
3
Using :include with has_one vs. has_many
While I''m happy to continue talking about the vagaries of Rails deployments, I
also need to actually build apps. :)
I may have discovered a bit of a problem with :include as it applies to has_one
associations. First, let me say that I''m a big fan of :include for reducing the
number of SQL calls. There are times when I wish it could go more than 1 step
away from the parent object, but I kind of understand why it can''t. Now, on to
my "discovery"...
Completely...
2006 Jul 17
2
Trouble with has_one
Hi,
I''m trying to write a blog application where each post has a single
header image associated.
My models:
----------
class Post < ActiveRecord::Base
belongs_to :blog
has_many :images
has_one :header
end
class Header < ActiveRecord::Base
file_column :image_path
end
class Image < ActiveRecord::Base
belongs_to :post
file_column :image_path
end
The controller targets I have used:
-----------------------------------
def uploadImage
Post.find(params[:id]).images.create(pa...
2009 May 25
4
after_create and has_one association bug?
Hello,
I''ve come across an issue that I''m sure is a bug when using after_create
with a has_one association, but I''m not 100% certain if I''m missing
something.
This is pretty much exactly the code I''m using. Two simple classes
(Account and Contact) and I create the contact after I create an account
(via after_create). I''m not passing in a "name"...
2007 May 14
0
building and saving has_many and has_one relations
Hi,
I am very confused about an aspect of has_one and has_many relations,
with regard to using the build method, and saving the belongs_to side.
It''s a somewhat long post. so please bear with me :)
First let''s consider the has_one scenario. Let''s say I have a student
who must own exactly one car:
class Student
has_on...
2006 Jan 31
4
has_one without inverse belongs_to
I have two models called entity and user.
The entities table has a column called users_id that contains the user
id of the user that created the entity.
In entity I have...
has_one :user
... as I want to be able to show the user who created the entity from
the entity object.
But this produces the following error...
Mysql::Error: #42S22Unknown column ''users.entity_id'' in ''where clause'':
SELECT * FROM users WHERE (users.entity_id = 1) L...
2010 Feb 01
3
validating both sides of a has_one relationship breaks pickle/machinist tests
A lot has been posted about validation issues with associations,
however, I don''t see this issue addressed specifically.
ex:
class Foo
has_one :schedule, :dependent => :destroy
validates_presence_of :schedule
class Schedule
belongs_to :foo
validates_presence_of :foo_id
this creates a circular dependency that breaks test frameworks like
pickle and machinist.
At first I was surprised a little that you can contsruct objects with
t...
2006 Mar 19
2
Multiple polymorphic belongs_to declarations
I have the following models:
class Card < ActiveRecord::Base
belongs_to :deck
belongs_to :front, :polymorphic => true
belongs_to :back, :polymorphic => true
end
class TextContent < ActiveRecord::Base
has_one :card, :as => :front
has_one :card, :as => :back
end
The conflicting has_one declarations don''t work. What I need is a way to
name the belongs_to associations ''front'' and ''back'', but have them go
through a single interface, which the T...
2006 Jan 18
2
categories/recipes & books/descriptions - has_many vs has_one => id question
People,
In the cookbook eg, categories has_many recipes but in a book eg, book
has_one description - doesn''t that mean that the id of the description
should be the same as the id of the book (instead of having it''s own
"description_id" in the book table?
Thanks,
Phil.
--
Philip Rhoades
Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3...