Displaying 20 results from an estimated 40000 matches similar to: "how to prevent child.save() when doing parent.save() in associations?"
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)
2006 Apr 20
4
Question about Associations
Hi all. 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
2006 Apr 12
0
Looking for a simpler model using :through associations
Hi. I''m building a setup where I have users and groups, and a user can
have an optional title in a group (eg. "owner"). I''m doing this using
"has_many :through" associations, using a Role model object as the join hub:
class User < ActiveRecord::Base
has_many :roles, :dependent => :destroy
has_many :groups, :through => :roles
end
class
2006 Jan 06
6
HABTM problem not saving all associations
Hello all,
I have an Order object that has and belongs to many Products and has
and belongs to many Loan Types. This is so I can select multiples of
each in my order entry screen via checkbox groups.
I''m having some trouble with saving multiple HABTM associations for a
single model object; only the first HABTM association declared in the
model will save during the initial @order.save
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 does get called
2006 Nov 05
0
Forms with associations and collections
Hi
Does anyone have a good pattern for using a sinlge form for a model
with multiple associations including some has_many''s?
For example I have a Property model with a has_one association an
Address model and a has_many association to a Highlight model. I would
like to update these all on the same page however if I render the main
Property form, then use a partial for the address and
2006 May 12
1
can I fake has_one :through?
I''m having a difficult time wrapping my head around a set of models
and their associations and seeing how 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
2013 Feb 06
0
inverse_of breaks for unsaved child associations during save of parent
I''m willing to write a patch for this issue:
https://github.com/rails/rails/issues/8893
But want to confirm that the new behavior makes sense before I make the
effort.
Essentially if you have an unsaved parent and child, and the parent''s
association to the child has an inverse_of declared on it. When the parent
gets saved, it updates the foreign key on the child, and this
2006 Jan 04
1
Multiple Associations With The Same Object
I have a Company object which has multiple Contacts
(has_many/belongs_to) I would like one of the contacts to be the
primary contact. What''s the best way to do this? Right now I have:
create table companies (
id int not null auto_increment,
name varchar(50) not null,
primary_id int null,
constraint fk_company_primary_contact foreign key (primary_id)
references contacts(id),
2006 Jan 06
0
bug rails activerecord association join
hi,
I found a bug in ActiveRecord association SQL INNER JOIN codes. in
has_many, has_one, habtm and belongs_to queries the foreign key is
determined from the table name of reflection and self. but that''s
worng because if you use a global prefix or suffix for your tables the
foreign keys will be in format of PREFIX_CLASSNAME_SUFFIX_id which is
wrong, because it breaks the advantage of
2007 Mar 01
0
association extensions - has_one
Hi,
I''m struggling with something that must be very common pattern, and
want to do it the correct rails/ruby way but am a bit dense !
I have users - an admin user, registered users and a guest user (using
ModelSecurity)
For many of linked associations I want the admin to be able to list or
edit all instances, the registered user only their own one and guest
none at all.
So somehow I
2006 Jul 03
0
Eager loading of ''is meant to be nil'' Associations
If I ''find'' using :include, and some included associations are null,
then looping through the array executing ''if obj[i].assoc'' accesses the
database for a second time (as obj.assoc is nil).
eg
Topic, has_many posts
Post has_one email
and
posts = Post.find(:all, :condition => ''posts.topic_id = 10'', :include =>
2006 Jan 30
1
Multiple associations between tables - do they ever work?
This is really bugging me. How can I get a second association to work
between 2 tables.
I have two models, User and Article. A user has many articles, and an
article belongs to one user. Then I have the "current article", which
is a the article that the user is currently viewing/editing. I want to
track/save this value, so they can come back to the same article if they
log out
2008 Apr 21
0
Lost Polymorphic Associations in development environment causes delegate :blah, :to => to fail
Hi All
I have been struggling with this issue for a long time now. And before
I began to describe the problem its important to know that this
problem happens only in Development environment.
I am using Rails 2.0.2, here are my associations
class User
has_many :comments
has_one :profile, :as => :profileable
delegate :email, :to => :profile
end
First call to comment.user.email works
2006 Jul 22
2
acts_as_automatic
Acts as automatic has two purposes:
1) provide acts_as_automatic act, which will automatically run the
appropriate methods when conventions are adhered to (e.g. if you have
category_id, it will assume that the model belongs_to :category). Only
belongs_to via something_id is currently implemented.
2) Allow the creation of associations.yml, which will hold associations
instead of inside each
2010 Oct 07
1
Question on polymorphic association
I have 3 models . Doctor, Patient and User
with following associations
class Doctor < ActiveRecord::Base
has_many :patients, :dependent => :destroy
has_one :user, :as => :userable
accepts_nested_attributes_for :user
end
class Patient < ActiveRecord::Base
belongs_to :doctor
has_one :user, :as => :userable
accepts_nested_attributes_for :user
end
class User <
2009 Feb 19
3
Associated child records not created on creation of a parent.
I am trying to create a record with association. Within the create
method, I would also like to pre-populate dependent (has_many) records
from a template in the database.
The child records are <u>mysteriously rolled back </u> if I insert
them in a loop. If I try to insert a single child record, it works.I
have read the books and tried to google my way out of this but am at a
dead
2007 Aug 03
0
acts_as_paranoid and Association Extensions (has_one troubles)
Hi... Isn''t there a proper way or hack/workaround for the following?
* For the example with unfortunate disabilities.. look below,,,
Usage of has_* (associationmethods) do {def with_deleted
AccociatedModel#with_scope} {Paranoid''s#find_with_deleted}...
The has_one associations give me a nil object when invoking it with
Model.association_OBJECT.with_deleted. Has_many does like
2007 Sep 05
8
Hi..Guys new plugin again
Hi..
Guys new plugin again
Foreign Key Associations is a plugin that automatically creates
associations based on foreign-keys. The associations are created at
runtime-ie. as your ActiveRecord models are loaded-so they will always
be up-to-date.
For example, given a foreign-key from a customer_id column in an
orders table to an id column in a customers table, the plugin
creates:
2008 Nov 19
3
Overwriting / Decorating ActiveRecord association accessor
Hi,
I am trying to overwrite the accessor of a has_many association.
Basically my class looks like this:
class Person
has_many :emails
has_one :link, :class_name => ''PersonProxy''
def emails_with_link
link.emails + emails_without_link
end
alias_method_chain :emails, :link
end
Now that works fine if I only access the collection like
>>