Displaying 20 results from an estimated 30000 matches similar to: "AR - Delete m:n Associations when deleting Record"
2006 Jul 22
2
How to SELECT from multiple talbes with AR#find and associations?
So I want to SELECT from multiple tables, so as to be able to do
"SELECT FROM foo, bar WHERE foo.id=bar.id AND bar.baz=23".
How do I do it with AR#find?
Also, I need this for associations. has_many et. al. have :finder_sql,
which is nice, but feels like a Pyrrhic: it''s basically doing the
association in pure SQL, defeating the purpose of AR.
--
-Alder
2006 Mar 15
1
An Association Problem: AR::Base.find{:include} and n-deep association traversal
Dear List,
I''m using Edge Rails.
I have a Model with Items for purchase. Each Item is of a certain brand.
Class Brand < ActiveRecord::Base
end
Class Item < ActiveRecord::Base
belongs_to :brand
end
The problem domain is that I''d like to create a separate Inventory
list of Items. This includes the date of entry into inventory and
date of exit from inventory (ie
2009 Aug 06
2
duplicate model object with associations ?
class Patient < AR::Base
has_many :aaas
has_many :bbbs ,:through=>:aaas
end
class Aaa < AR::Base
has_many :bbbs
end
-------
i want like this
@patient=Patient.find(id)
@new_patient=@patient.duplicate
now the
@new_patient should have their own copy of patient,aaas,bbbs (new
records in corresponding tables)
there is one options is that
we can create seperate object for each
2006 Feb 09
2
Polymorphic Associations
If anybody on edge familiar with this could help, that would be "great" :)
I have different types of things I want to be "reviewable". So, instead of
having a slew of HABTM... this new Polymorphic Associations schtick seemed
like the best solution. I have everything setup as I thought it should be
(so I think)... but it doesn''t appear to be working correctly. Here
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
2013 Mar 30
1
How to use group in nested associations
The below query fails:
Timesheet.joins(:time_entries).select("timesheets.*,
sum(time_entries.worktime) as total").group("timesheets.start_date")
The models have the following relations:
Timesheet < AR
has_many :activities, dependent: :destroy, inverse_of: :timesheet
has_many :time_entries, through: :activities
accepts_nested_attributes_for :activities,
2006 Nov 04
0
deleting associations
I have two class, client and contacts
A client has many contacts
class Client < ActiveRecord::Base
has_many :contacts
end
class Contact < ActiveRecord::Base
belongs_to :client
end
--------------------------------------------
I have the next data
Table clients
id name
1 John
Table contacts
id email
1 john@yahoo.com.ar
2 john@gmail.com
3
2007 Aug 23
3
AAF: find_by_contents on AR Association Total Hits
I seem to be getting some behaviour thats unexpected (for me anyway)
when using find_by_contents on an ActiveRecord has_many association. The
results that are returned are only the records that belong to the model
returned, but the total_hits that are being returned appear to be for
the whole table.
e.g.
class Book < AR::Base
has_many :pages
end
class Page < AR::Base
belongs_to :book
2006 Jun 17
2
how to delete a parent record and child records?
Wow, got myself lost as *soon* as I left the hand-holding exercises in
the books.
I''m trying to set it up so when I click the (scaffold-generated)
''destroy'' link on one (of a list) of parent MySQL records, the children
records in a different table get deleted also.
Very simple setup: my tables are ''customers'' and ''accounts''
class
2009 Jan 21
1
AR to_xml problem with associations, Builder::XmlMarkup#to_a
When I do a to_xml(with a block) on an association of a model I got a
Builder::XmlMarkup#to_ary should return Array
Example that does not work.
@intervenant = Intervenant.find(params[:id])
output = @intervenant.to_xml( :skip_types => false, :dasherize =>
false) do |xml|
@intervenant.individu.to_xml( :builder => xml, :skip_instruct =>
true, :skip_types => false,
2006 Jan 10
5
Select Tag and Associations
Hi there,
I''ve been working on this for awhile and have finally decided to ask
for a little guidance.I have a slight problem trying to save a
selection.
I have two models:
A "Posting" has_many "Categories", and a "Category" belongs_to one
"Posting". With that said, in the posting model I have "has_many
:categories" and within the
2006 Feb 03
1
nested has_many associations
My question is whether the rails helper methods for handling associations can
be nested (see examples below). I suspect not as currently I''m
getting "unknown method" if I try and use two associations in one call.
The essence of the code is
class User << ActiveRecord::Base
has_many :items
class Items << ActiveRecord::Base
has_many :reservations
belongs_to
2006 Jul 02
2
Problems implementing a N:M table that contains actual data
Hi everyone,
I''m currently finishing the planning phase of a small project I want to
create. I read a bunch of tutorials about ActiveRecord and the
different way to link your "tables" with belongs_to, has_many,
has_and_belong_..., etc... However I''m stillnot sure if what I''m trying
to do will work *well* with ActiveRecord.
So enough talk,
2006 Aug 16
1
Naming rights_roles join model using has_many :through and polymorphic associations
Hi.
I have a couple of best practices questions regarding polymorphic
associations, naming join tables and user permissions.
Currently I have implemented the user authentication model from the
rails recipes book. Basically it goes something like this:
MODEL CLASSES:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
class Role < ActiveRecord::Base
2006 Apr 04
1
Manipulating has_many :through associations
Hi,
I haven''t done any associations before, so please forgive my incorrect
use of terminology...
If I set up a has_many :through association, can I manipulate the
associated instances directly (like an Array), or do I need to
manipulate the join model?
For example, suppose I have two classes and the associated join model:
class foo
has_many :foo_bars
has_many :bars, :through
2008 Apr 11
2
Validating an ActiveRecord object and its has_many :through associations
Considering an object with several has_many :through => associations,
what is the ''best'' way to handle validations?
As an example:
class Student < ActiveRecord::Base
# some attrbutes like
# :name
# :grade
# relationships
has_many :students_assignment, :dependent => :destroy
has_many :assignments, :through => :students_assignment
has_many
2006 Jun 07
1
Calling find on AR class, order by association count?
I suspect that this is going to wind up trivial and I''m going to slap
myself in the head...
Suppose I have User and Comment AR classes.
User has_many :comments, Comment belongs_to :user
I want to do a User.find, which returns User objects in order of which
user has the most comments. I suspect that this involves renaming the
COUNT(*) results of a sub-select so that I can order by
2013 May 22
3
rails 4, active record: appending to has_many :through does not create join model anymore?
hi folks.
on rails 4, can somebody confirm that pushing model instances onto a
has_many :through relation does not create the necessary join model anymore?
class Reader < AR::Base
belongs_to :post
belongs_to :person
end
class Post < AR::Base
has_many :readers
has_many :people, through: :readers
end
@post.people << Person.new
so,
2006 Mar 24
2
Change to has_many :through associations
Hi everyone. I''ve made a default change to has_many :through
associations that I felt was important to make before they''re
released. This is after some tickets and confusion I''ve seen
regarding has_many :through.
class Connection < ActiveRecord::Base
belongs_to :user
belongs_to :channel
end
class Channel < ActiveRecord::Base
has_many :connections
2006 May 11
6
ActiveRecord associations
Can someone clarify this for me? Suppose I have two tables like:
class Company < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :companies
end
I understand that the belongs_to causes related companies to be read
from the db when I ask for a category. I suspect SQL something like
select * from categories, companies
where category.id