Displaying 20 results from an estimated 10000 matches similar to: "has_many working correctly only on reload!"
2006 Jul 10
10
has_many :through and foreign key parameters
I just took my HABTM and turned it into a :through since my join table
has another "non-joiny" attribute.
I went from this:
has_many_and_belongs_to :jobs, :join_table => ''tablename'',
:foreign_key => ''x'',
:association_foreign_key => ''y''
to this:
has_many :jobs, :through =>
2006 Apr 20
3
has_many :through with has_many/has_many join models
It seems that using a join model that joins with two has_many''s will
fail to generate proper SQL
class StudentSemesterRecord < ActiveRecord::Base
belongs_to :semester
has_many :discipline_records, :through => :semester
end
class Semester < ActiveRecord::Base
has_many :student_semester_records
has_many :discipline_records
end
class DisciplineRecord <
2006 May 10
7
has_many :through scope on join attribute
Hi
I have a has_many :through. It''s a basic mapping of
Project
id
....
User
id
....
TeamMembers
project_id
user_id
role
What I would like to do is have different roles so I can have in the project
model
has_many :core_members, :through => :team_members, :source => :user
but I would like to limit this to only those with the "core" role in the
team members table for
2006 Jul 10
7
What is has_many :through really buying you over HABTM?
So having just learned how to do has_many :through as opposed to HABTM,
and then, being concerned that I wouldn''t get it to work, I started
thinking about these two approaches.
It seems to me that the _only_ problem that the HM:T (has_many :through)
approach solves that HABTM doesn''t is the issue of the potential
collision of id columns between your join table and one of
2006 Apr 21
3
polymorphic,has_many through can not work?
Josh Susser tells in his blog that the opposite direction of polymorphic
will get into trouble together with has_many through.
This is the url:
http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through
I do that according to Josh Susser''s procedure:
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, :polymorphic => true
belongs_to
2006 Jul 10
18
Deleting join association of has_many :through
I''m trying to use has_many :through, since my join model deserves being
more than just an intersection table.
But when I try to break the association, the break only seems
"temporary":
Let''s say my two tables are Users and Colors, and the join model is
Favorites.
user = Users.find(1)
user.colors.length
>> 2
c = user.colors.first
>> #<Color:....>
2006 Aug 12
7
Collection assignment to a has_many :through
I''m working on a simple photo gallery in rails, it seems to be a good
project for a newbie.
I have photos and categories, many-to-many association. It worked well
with HABTM. Then I decided that it would be good to be able to change
order of the photos so that thumbnail pages would look less chaotic.
So I created a Layout model which is a join model (or whatever it is
called) that
2006 May 28
7
Self-referential has_many :through relationship
Hi,
I have a self-referential has_many :through relationship setup to
track relationships between users. Basically relationships are
modeled as a join table with an extra column ''relation''.
create table relationships (
user_id integer unsigned not null,
friend_id integer unsigned not null,
relation char(1) not null,
)
--- relations ---
f = friend
r = request to
2006 Jul 28
2
has_many :through with multiple paths
Hi all.
I am currently thinking about how to do the following:
I have the following models.
Team
Person
Team has various positions (manager, programmer etc.) I would like each
one of those positions to reference one or more Person records. A single
Person record could be on 6 different teams at the same time in
different roles. the same person could even be on the same team multiple
times
2006 May 21
3
has_many :through with a polymorphic join
Hi,
Four tables: users, user_counties, uk_counties and us_counties.
Each user has many counties, and each county has many users, so I
decided to make user_counties a polymorph, so it can have counties
from different countries (each country requires a completely different
set of tables with a completely different set of properties, that''s
why there''s one table for uk_counties and
2006 May 23
2
has_many :through extra domain model question
If I have a model called Newsletter with:
has_many :subscribers, :through => :subscriptions
and wanted to list all the subscribers, but show what level their
subscriptions was (level is in subscriptions table), how would I best do
that?
Do I have to use my own select to get what I want or are the attributes
from subscriptions available to me through the join somehow (I am
suspecting not)?
2006 Aug 03
12
More than one has_many :through association between the same 2 models
I wonder if you can have more than one has_many :through association between
2 models.
For example...
I have a model Teacher and a model Class
Now, 1 Teacher works in many Classes, right?. So I need a join model like
class Work < ActiveRecord::Base
belongs_to :teacher
belongs_to :class
end
But I also would like to know if a teacher CAN teach a class before I
2006 Dec 05
4
has_many with :uniq not working for me
Hi all,
I have a relationship (no really!)
class RiskMatrix < ActiveRecord::Base
has_many :severities, :order => :position, :uniq => true
end
class RiskFactor < ActiveRecord::Base
belongs_to :risk_matrix
validates_presence_of :descriptor, :example
validates_uniqueness_of :descriptor, :example, :scope=> :risk_matrix_id
end
class Severity < RiskFactor
2006 Jun 30
2
has_many through - getting the parent
has_many :through is great. Is there anyway to find the association
that it went through?
Example:
class Student
has_many :projects
has_many :grades, :through => :projects
end
student.grades[3].project
Is it clear what I''m asking? How would this be done?
--
Posted via http://www.ruby-forum.com/.
2006 May 23
7
self-referencing has_many
Having a devil of a time finding records in a self-referencing has_many
table relationship.
Everything is working find looking at the has_many and the belongs_to
relationship.
But, when I try to find all "orphans", records that are neither a parent
nor a child, I can not find a query that work in ActiveRecord.
This query works in MySQL:
SELECT * FROM templates
LEFT JOIN templates
2006 Jun 08
3
has_many :through updates on delete.
I have a model that looks like this:
class Actor < ActiveRecord::Base
has_many :member_groups, :foreign_key => ''member_id'', :class_name =>
''GroupMember''
has_many :groups, :through => :member_groups, :source => :group
end
class Group < Actor
has_many :group_members, :foreign_key => ''group_id'',:dependent =>
2006 Aug 02
2
Self-Referential has_many :through
Hello all.
I am trying to create a self-referential has_many :through. I used the
following site as a guide
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through
but it still doesn''t appear to be working. I have two models. Person and
Relationship. A person has many contacts (Which is another person)
through relationships
class Person < ActiveRecord::Base
2006 Jul 02
1
prevent duplicate inserts with has_many :through ??
Does anyone have an efficient/elegant way to prevent duplicate inserts
when using a has_many :through relationship.
So I have something like this:
class User < ActiveRecord::Base
has_many :favorite_teams
has_many :teams, :through => :favorite_teams
end
class FavoriteTeams < ActiveRecord::Base
belongs_to :user
belongs_to :team
end
class Teams < ActiveRecord::Base
has_many
2006 Jul 05
7
HABTM join table has an "ID" column - is this an issue?
All,
I''m building model objects for existing tables that I cannot modify.
In AWDWR, Dave says "Note that our join table has no id column...The
second reason for not including an id column in the join table is that
AR automatically includes all columns from the join tables when
accessing rows using it. If the join table included a column called id,
its id would overwrite the id
2006 Mar 29
4
:through alternate
I''d like to use :through to create a web of associations like:
class Thing < ActiveRecord::Base
has_many :child_things, :through => :thing_thing
has_many :parent_things, :through => :thing_thing, :some_other_option?
end
class ThingThing < ActiveRecord::Base
belongs_to :thing
belongs_to :child_thing, :class_name => ''Thing'', :foreign_key =>