Displaying 20 results from an estimated 20000 matches similar to: "Help setting up relationships needed"
2011 Feb 07
2
Search HABTM relationship in ActiveRecord
Consider the following scenario:
Book has_and_belong_to_many Category.
In other words, a category may have a lot of books, and a book can
have more than one category.
What is the best practice to query all books related to some specified
categories.
For example, if I have following books
"From Java To Ruby" - Category: Java, Ruby
"Ruby Programming" - Category: Ruby
2006 Aug 11
2
User Favorites
Hi there,
I''m currently creating a simple (at least so I thought) structure of
user favorites. Basically users can add certain ''objects'' to their
list of favorites.
Here is my current structure (simplified)
class User
has_many :favorites
end
class Book
belongs_to :favorites
end
class Favorite
belongs_to :user
has_one :book
end
The table favorite just has a two
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 Mar 19
3
Table belongs_to either of two other tables?
Suppose I have a site about books and authors, and users can leave
comments on either a book or an author.
I''d want to have a table called "comments", which could belong to either
an author OR a book.
In my structure I wouldn''t be sure whether to include an "author_id" or
a "book_id", or both.
What would be the best way to handle this
2006 Jan 18
2
categories/recipes & books/descriptions - has_many vs has_one => id question
People,
In the cookbook eg, categories has_many recipes but in a book eg, book
has_one description - doesn''t that mean that the id of the description
should be the same as the id of the book (instead of having it''s own
"description_id" in the book table?
Thanks,
Phil.
--
Philip Rhoades
Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney
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 Dec 11
1
Index help on Polymorphic Associations
Hello,
I''m a newbie to aaf and rails and I hope anyone can help me with this.
I have the following Models:
class Project < ActiveRecord::Base
acts_as_authorizable
acts_as_audited :except => [:created_by, :updated_by ],
:user_class_name => ''AuthenticatedSystem'', :user_method =>
''current_user''
acts_as_ferret :fields => {:name =>
2006 Apr 24
9
Confusion with expressing many to many relationship
Hi folks, I am in the process of converting an existing non-rails
application to rails, and am not sure what the best approach would be
for specifying the relationship between the two sets of data.
I''ll describe the existing table structures first.
Table 1: Urls
Each url has a unique id and two lists of Phrases, these are currently
setup as varchars with "," delimitation -
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 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 Dec 20
7
has_many :through does it support collection_singular_ids ?
Good evening list members,
I have the following model:
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => : authorships
end
Should I be able to do the following?
Author.find(:first).book_ids = [1,2,3]
I ask, because it doesn''t. The docs state that it should, but it
doesn''t specialise in the case of a :through
This
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 =>
2006 Jun 07
10
habtm "AND" find conditions
I have two tables and a join table for them
e.g. books, authors in a many to many relationship (habtm) and a join
table books_authors.
I can successfully search for a book that has
"author.id = 2 OR author.id = 4"
but I am unable to search for
"author.id = 2 AND author.id = 4"
This is because the result of all the joins only has one author.id
column so no single row has
2006 Jun 06
6
Linking two tables using a lookup table
Hi,
How do I associate two different tables which have a lookup id (table)
as common.
I tried the has_many :table2, :through => look_up_table in my table1
model.
Any hints?
Thanks, Hari
--
Posted via http://www.ruby-forum.com/.
2006 Jul 04
2
has_many working correctly only on reload!
I have this code that is using svn externals with rails EDGE working
fine for the past couple of months. A couple of days ago, this code
died. Even after deleting vendor/rails, this code doesnt work:
class Tag < ActiveRecord::Base
has_many :taggings
has_many :events, :through => :taggings
has_many :users, :through => :taggings
end
class User < ActiveRecord::Base
# Virtual
2006 May 27
7
How should I select rows from a join-model based on more than one association?
A concrete example:
We''re building a system to organize the information about workshops
being held in various conventions. A convention has more than one
workshop, and each workshop can be held in more than one convention.
Also each workshop has a host, who is specific to a workshop being
held in a specific convention.
For example, Matz may host an "Introduction to Ruby"
2008 Jun 15
7
Getting "no block given" on find method - rails 2.1
I am trying to do a simple find through the current_user but get an
exception. Either someone spiked my Starbucks coffee and I am screwing
up all over the place or there is something wrong.
def index
@league = current_user.leagues.find(13)
end
# Error
LocalJumpError in Admin/schedule todosController#index
no block given
If I pass a block to it it works fine, but it is just messy:
=====
2006 Nov 02
4
Still Having Problems With :through When Going To Same Table... Help... please :-(
I am having a problem with doing a :through that goes back to the same table.
The following are my two classes:
>>>>>
class User < ActiveRecord::Base
has_many :spanks
has_many :spanked, :through => :spanks, :source => :spanked_user
has_many :was_spanked_by, :through => :spanks, :source => :user
end
class Spank < ActiveRecord::Base
belongs_to :spanker,
2006 Apr 25
4
belongs_to :through
belongs_to :through
Why is that not possible?
We should have:
Street
belongs_to :city
belongs_to :country, :through => :city
--
Posted via http://www.ruby-forum.com/.
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 =>