Displaying 20 results from an estimated 10000 matches similar to: "has_many :through"
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 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 04
1
Has_many :through with checkboxes?
Just when I started getting the hang of has_and_belongs_to_many
relationships, I realize that I should probably convert several of my
app''s joins to has_many :through type relationships.
One example, pairing users with categories via subscriptions, where
each user can be the "owner" of a category, has given me particular
trouble when I try to update a user''s
2007 Apr 03
2
has_many :through associated object ids assignment error
Hi,
I have the following situation:
class Article < ActiveRecord::Base
has_many :readings
has_many :users, :through => :readings
end
class Reading < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
Now what I''m trying to do is:
article = Article.new
article.save!
# user ids 1,2,3 exist in database, as does the article
article.user_ids = [ 1, 2, 3 ]
But I
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
2007 May 04
1
habtm and also has_many of the same things
Hi,
our app is a situation where users can upload files and also mark files
uploaded by other users as their favorites. So for the favorites we are
using a simple many-to-many with a tie table.
So in attempting that it looks like
class User < ActiveRecord::Base
has_and_belongs_to_many :media_file
has_many :media_file
end
class MediaFile < ActiveRecord::Base
2006 Jun 26
4
has_many :through
first, the models:
class Listing < ActiveRecord::Base
has_many :keyword_links
has_many :keywords, :through => :keyword_links, :conditions => "active =
1"
end
class Keyword < ActiveRecord::Base
has_many :keyword_links
has_many :listings, :through => :keyword_links, :conditions => "active =
1"
end
class KeywordLink < ActiveRecord::Base
belongs_to
2006 Jul 11
4
checkboxes with a has_many :through relation
All,
I''m populating a view with checkboxes in a list. I want to be able to
assign to my has_many relationship using the post values of these
checkboxes. Essentially, I want to populate the relationship table
which will create the correct join values in my model.
Here is my check_box_tag code:
<%= check_box_tag("@current_job[target_list_ids][]",
2006 May 18
3
Model Madness: habtm vs through
This has me scratching my head:
a Person has many Things
a Person has many Collections
a Collection has many Things
...Things in a Collection are ordered
...Things in a Collection can be related to (created by) any User
...a collection has additional attributes, such as a name etc
I am confused about habtm in rails (especially when using
acts_as_habtm_list) vs. going the :through route.
2006 Apr 25
3
belongs_to and has_many
I know this has been rehashed many times, but I don''t get it.
I can''t find it documented to were I understand all the parts. I can
make this work using Rails database design conventions, but in this case
I am not able to do that.
I find examples, but the ones I find do not follow the conventions.
They also do not give enough information to understand what is going on.
In
2007 Apr 04
4
Order a find using a has_many association
I''m trying to order a find through a has_many association. I have a
model called SalesOrder. Each sales order has things like amount,
description, etc. I use the following to get the Sales Orders:
@sales_orders = SalesOrder.find(:all)
And I display them with:
<td><%= link_to sales_order.id.to_s, :action => ''show'', :id =>
sales_order.id
2006 Mar 19
1
some strange behavior for has_many with STI
Hi all,
I''m having some trouble with a has_many association on a table using
the single table inheritance model that I''m hoping someone can help me
with. The schema for the table is:
CREATE TABLE `comments` (
`id` int(11) NOT NULL auto_increment,
`user_id` int(11) default NULL,
`type` varchar(20) NOT NULL default '''',
`type_id` int(11) NOT NULL default
2011 Apr 06
2
Wish List: has_many :dependent => {}
I am after a nice way to disable (not destroy, delete nor nullify) all
associated records. I''m a hoarder, I don''t want to get rid of anything but I
want to stop records being visible.
I have a disabled boolean in my tables and if I disable the top record in
the association (say the association is 5-6 levels deep), I would like to
cleanly disable its children.
I''ve
2012 Jan 30
4
Reaching into a has_and_belongs_to_many - will :through work?
Hi. I''m hoping somebody can tell me that I''ve just gotten my syntax
wrong and I can in fact do what I want to do.
I''ve got a Group, and a Group has_many Campaigns.
Campaign has_and_belongs_to_many Users (and, naturally, vice versa).
So I''ve got a groups table, a campaigns table (that has a group_id), a
campaigns_users join table (with campaign_id and
2006 Jan 06
1
has_many with :finder_sql question
hi all,
I have a Class, Client, which has_many projects (Project class). the
projects depend on a session_id variable, however. So the question
is, how can I do the following:
has_many :projects,
:finder_sql => "SELECT p.* FROM projects p INNER JOIN
projects_users pu ON pu.project_id = p.id WHERE pu.user_id =
#{session[:user_id]}"
The problem is that I need to filter a
2006 Apr 28
4
Call for patterns...
Hi group,
I was wondering if anyone more experienced could help me to find a good
pattern for two things:
1. How to group controllers. Example:
We have an admin panel with:
user_managment_controller.rb
priv_managment_controller.rb
widget_controller.rb
User panel with:
mystuff_controller.rb
mytags_controller.rb
profile_controller.rb
And frontend with:
widget_controller.rb
etc...
How do I group
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 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
2008 Apr 18
3
has_many and belongs_to with non-primary foreign keys
Hi,
I''m having a bit of trouble with my first Rails app.
----
I have two tables:
create_table :items do |t|
t.column :created_at, :timestamp
t.column :user_id, :int
t.column :text, :text
end
create_table :users do |t|
t.column :user_id, :int
t.column :name, :string
end
----
I''m trying to use the "user_id" field to link both tables, with each
user
2005 Dec 15
2
HABTM being tricky
I''ve just been bodyslammed by a problem with has_and_belongs_to_many -
as far as I can tell, if it doesn''t appear at the top of other
relationship definitions it doesn''t seem to work right. For example:
class Artist < ActiveRecord::Base
has_and_belongs_to_many :genres
has_many :albums
has_many :videos
has_many :cds
has_many :collections
end
..works fine