Displaying 20 results from an estimated 40000 matches similar to: "Serializing has_many associations and ActiveRecord objects"
2011 Jul 26
1
ActiveRecord has_many associations
Given the models Country, State, City and Person as follows.
class Country < ActiveRecord::Base
has_many :states
end
class State < ActiveRecord::Base
belongs_to :country
has_many :cities
end
class City < ActiveRecord::Base
belongs_to :state
has_many :people
end
class Person < ActiveRecord::Base
belongs_to :city
end
Is there any way that doesn''t allow to delete
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 Jul 17
14
REST Relationship Models
I''m trying to figure out an elegant way to do this:
I have the following three tables:
people, employer, employees
And consequently the following three models:
class Person < ActiveRecord::Base
end
class Employer < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :person
belongs_to :employer
end
I want to be able to say:
2006 Mar 13
8
Nested find(:all, :include => ) statements
Is there a way for me to do:
OrderItem.find(:all, :include => [:user, :product => [:supplier]]
So I don''t have a supplier_id on my order_item, but have it on my
product, which is part of order_item.
Joerg
--
Posted via http://www.ruby-forum.com/.
2006 Sep 26
0
ActiveRecord, has_many association and object graphs
I am still using Rails 1.0...
I have a simple has_many association:
class Round < ActiveRecord::Base
has_many :scores
end
class Score < ActiveRecord::Base
belongs_to: round
end
When I do something like this (contrived for the sake of clarity):
rr = Round.find(params[:id])
rr.scores.each { |s|
logger.debug("object_id=#{s.round.object_id}")
}
I see a database query on
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 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 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
2010 Oct 04
2
newbie q on activerecord - has_one and has_many on same table?
Hi I have a table where I need to identify one of the children as the
"prime" child.
So, its like:
Parent
has_many :Child
has_one: Child :as :prime_child (?)
or something like that? Do I put something like prime_child_id in the
Parent table?
Thanks.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this
2008 Apr 24
2
Dynamic finders in has_many associations
I have these 3 models.
class Ivr < ActiveRecord::Base
has_many :klusterings, :dependent => :destroy
has_many :klusters, :through => :klusterings, :uniq => true
end
class Kluster < ActiveRecord::Base
has_many :klusterings, :dependent => :destroy
has_many :ivrs, :through => :klusterings, :uniq => true
end
class Klustering < ActiveRecord::Base
belongs_to :kluster
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
2006 Jan 15
6
Saving one-many associations (elegant solution please)
What is the most elegant way to save new one-many associations?
order = Order.new(:name => "My Order")
order.line_items << LineItem.new(:product_id => 1, :quantity => 2)
order.line_items << LineItem.new(:product_id => 2, :quantity => 5)
order.save
The above - which is by far the most elegant way of putting it - doesn''t
work for me - the line_items
2006 Apr 14
1
ActiveRecord and multiple associations
Hello,
I have a Company that has_and_belongs_to_many Persons. A Person
has_many Emails. I want to display each Person''s Email for each
Company, so I try this:
companies = Company.find(:all)
for company in companies
persons = company.persons
for person in persons
puts person.email
end
end
Unfortunately, this doesn''t work correctly. Let''s say the id of
2010 Mar 10
0
Rails ActiveRecord associations autosave option
I have the following associations.
class Document < ActiveRecord::Base
has_many :document_sections, :dependent => :destroy, :autosave => true
has_many :document_items, :through => :document_sections
end
class DocumentSection < ActiveRecord::Base
belongs_to :document
has_many :document_items, :dependent => :destroy, :autosave => true
end
class DocumentItem <
2009 May 12
4
has_many :through and scopes: how to mutate the set of associated objects?
I have a model layer containing Movie, Person, Role, and RoleType,
making it possible to express facts such as "Clint Easterbunny is
director of the movie Gran Milano".
The relevant model and associations look like this
class Movie < ActiveRecord::Base
has_many :roles, :include => :role_type, :dependent => :destroy
has_many :participants, :through => :roles, :source
2006 Jul 24
1
problem with has_many associations
Hi all,
I have some problems with a few temporary objects, this is the relation
between them:
A -(1,n)-> B -(1,1)-> C
B -(1,n)-> D
C -(1,n)-> D
I have a "wizard" to create "A" objects, I''m storing everything in the
session, something like this:
a = A.new
session[:a_object] = a
...
b = B.new
b.c = some_c_instace
a.bs << b
...
d = D.new
b.ds
2005 Jul 28
0
ActiveRecord, computed values, and associations
Is there some sort of good pattern for dealing with computed values with
Rails/ActiveRecord?
A simple example: 3 tables, classes, students, and student_class_records
(habtm style relation but contains a good deal of additional state so is
modeled explicitly)
Something like select the 10 classes with the most students. Easy enough
in SQL. So I''m using a lot of find_by_sql and
2006 Jan 26
1
Explanation on Activerecord Associations
A while back, I had some doubts on the use of associations. I still
haven''t got a satisfactory guidance so that I can spring ahead with the
project. I am trying to understand from a ruby point of view on doing
things. Basically I have a controller that gives me a list of students.
And each student may have say home address and mailing address. Hence I
can model as shown below. How do I
2006 May 17
2
ActiveRecord::Associations ER diagram/macros
Hello,
I found an interesting ER diagram on the "Module:
ActiveRecord::Associations::ClassMethods" page of the Rails Framework
Documentation :
http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
At the beginning we can read ? Associations are a set of macro-like
class
methods for tying objects together through foreign keys. They express
relationships like
2006 Jan 22
3
Download an Image using Net::HTTP
Hi,
I need to download an image, and I''ve mucked about with Net::HTTP
resp = Net::HTTP.get_response(''www.mydomain.com'', ''/test.jpg'')
f = File.new("test.jpg","w")
f.write(response.body)
And various other combinations using HTTP.get etc. Anyway, I get an
image, but it looks very psychedelic. This is obviously not the correct
way.