Displaying 20 results from an estimated 60000 matches similar to: "How to save has_one belongs_to models?"
2010 May 11
1
has_one/belongs_to -- accessing the subordinate
With a has_one/belongs_to relationship, what''s the best way to guarantee
that the belongs_to object gets created and is accessible alongside the
has_one object? I *think* the after_create callback is a good choice,
but I discovered an oddity while trying it.
F''rinstance, if every horse has a carriage:
============
ActiveRecord::Schema.define do
create_table(:horses) {|t|
2008 Apr 29
4
Default values for belongs_to and has_one
There really doesn''t seem to be a *declarative* way to define a default
value for belongs_to and has_one associations
class Person < ActiveRecord::Base
belongs_to :address
end
p = Person.new
p.address.city = ''Atlantis'' # Bummer!
Of course, there''s an easy way to avoid this
class Person < ActiveRecord::Base
belongs_to :address
2011 Jul 07
0
has_one through belongs_to
Hi.
My model data is the following:
- Place
- User
- Checkin
Instead of having a Checkin that belongs to both User and Place, I chose to
add another model, Program, that allows to list all the places a user can
checkin into, and also hide the checkins he''s no longer involved in (= the
user sign out of a specific program).
That give me crazy relationships:
Program has_many Checkin
2006 Sep 27
5
Question about has_one
I have a question about regarding the use of ''has_one'' in this scenario:
Schema:
User
----
id
first_name
last_name
UserMessage
----
user_id
message_id
Message
----
id
Assuming business rules dictate that a Message can have at most ONE User
(let''s assume a message can be created without a user associated to it),
these are my questions:
1) How would I use has_one
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_one(:car)
end
class Car
belongs_to(:student)
2008 Jun 12
1
unidirectional belongs_to polymorphic
The setup:
class Location::Base < ActiveRecord::Base
set_table_name :locations
...
end
class Location::Address < Location::Base
...
end
class Location::Airport < Location::Base
...
end
class Person < ActiveRecord::Base
belongs_to :location, :polymorphic => true, :class_name =>
"Location::Base"
...
end
I want a unidirectional belongs_to relationship. That
2011 Oct 20
2
Access other model attributes directly with has_one, belongs_to ?
So, I have 2 models A and B that share 5 common attributes and all other
attributes are different. So I wanted to extract these 5 out into one
common table and use has_one, belongs_to to knit it back together. So
now there are 3 tables with 1 having the shared properties, we''ll call
this table C.
Table A
id
dollar_amount
Table B
id
quantity_on_hand
Table C
id
version
My question is,
2007 Apr 14
0
has_one with reference to the one stored in this object's table?
I''m trying to build a system for generically storing an object graph,
on top of AR itself. The idea is to define one base class, and use
AR''s automatic single table inheritance to have it create the right
class.
Here''s what I have so far:
table generic_objects:
id int(11) not null auto_increment primary key
type varchar(255)
table generic_fields:
id
2006 Feb 20
1
belongs_to, has_one, has_many question (again?)
Hi All,
Probably this has been asked numerous times, I apologize already!
Can somebody point me to a good tutorial on how Rails works with
relations?
I know about database design and normalization, I also know about
programming in general (and OOP for that matter).
Only thing I can say is that I''m following the "Four days on Rails"
tutorial by John McCreesh.
He writes
2006 Apr 17
2
has_one problem in a form
Hello all,
I have a syntax question. How does one refer to a field brought in via a
has_one/belongs_to relationship in a form helper? Basically, I have two
models:
class Resource < ActiveRecord::Base
has_one :other_thing
end
class OtherThing < ActiveRecord::Base
belongs_to :resource
end
It has the normal id-resource_id foreign key relationship. OtherThing has
two fields, plus the
2006 Mar 23
3
when (not) to use belongs_to
I know you get a lot of questions like this, but I couldn''t find one which
answers exactly what I''m after. I''m reasonably new to rails, and I''m having
a bit of trouble with when to use belongs_to. On the rails wiki (
http://wiki.rubyonrails.org/rails/pages/belongs_to), it says "In general,
the Foo model belongs_to :bar if the foo table has a bar_id foreign
2006 Apr 12
0
Sorting with SQL for has_one, belongs_to relationship
I''ve been using this sort helper to have sortable tables.
http://wiki.rubyonrails.com/rails/pages/Sort+Helper
I''ve encountered a situation where I need to sort a column that is not
part of a table, but it''s related table.
For example, say I have db tables called foos and bars. Foos have
fields called name and title. And bars have fields called note and
foo_id. Foos
2007 Aug 21
4
[AWDwR] Confused about has_one and has_many
Heya everyone!
I am quite new to Rails and I am reading AWDwR. I am at Task E "Check
Out!". I understand it quite well, but I have a problem with the last
playground exercise: Add a Table which contains the payment types.
All goes well, but I am not sure whether I should use has_many or
has_one(as in the Playground wiki at
http://wiki.pragprog.com/cgi-bin/wiki.cgi/PT-E-3).
I use the
2006 Jul 06
4
has_one, belongs_to - I am just not getting it
Let me preface by saying i''m a total noob at ruby/rails. :-)
For some reason, I am just not grasping the concept of has_one &
belongs_to...
I''m working on a project that includes user authentication, and 3
different types of user profiles.
User profiles are either "fan", "band", or "venue" objects, and user
logins are "user"
2006 Nov 04
0
one to many relationship; has_one
I''m new to ruby and rails and as a result am a bit lost on implementing
a one to many relationship. In my DB I have 2 tables contents and
locations, contents contains the foreign key location_id. I generated a
content and a location model using rails scripts and then edited the
models as follows
class Content < ActiveRecord::Base
has_one :location
end
class Location <
2014 Apr 19
0
How to has_one/belongs_to association only return the objects unassociated in combobox?
I have two models (Car and Driver) and a combobox to select which driver
belongs to which car. I want the combobox just show the objects that have
not yet been associated.
# vehicle belongs_to: driver
# driver has_one: vehicle
# simple_form # vehicle/_form.html.haml
= f.association: driver, label_method: :name, value_method: :id
How to ensure validation before saving, to avoid problems of
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
2006 Mar 23
4
belongs_to more than one model
Suppose I have one table:
states
id
statename
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 <
2007 Oct 08
7
belongs_to association doesn't work with _id set
Hello there,
I''m developing an application which has models like this:
class Vehicle < ActiveRecord::Base
belongs_to :internal_dim, :class_name => "Dimension"
def before_create()
idim = Dimension.create
self.internal_dim_id = idim.id
#self.create_internal_dim # Doesn''t work
end
end
class Dimension < ActiveRecord::Base
2007 Jan 19
0
Do transactions fail with polymorphic has_one relationships?
I have an AR::Base descendant with a complex object graph beneath it,
like so:
ArtisanQuoteInput has_one QuoteInput and then QuoteInput has several
objects attached to it. Note that the relationship between
ArtisanQuoteInput and QuoteInput is polymorphic, so the relationship in
A is written
has_one :quote_input, :as => :program_quote_input
If @artisan_quote_input is an instance of