Displaying 20 results from an estimated 10000 matches similar to: "has_many and belongs_to with non-primary foreign keys"
2010 Aug 18
6
Once I added this HABTM, one of my 'through' relationships, on a non-habtm model, seems to have broke?
I''m a rails newb and have been Googling about this, but I''m still stumped.
Not showing everything here, but basically it should be a pretty common
setup so I''m sure others know what I''m doing wrong.
- A meter can belong to many meter_groups
- A meter_group can have many meters.
- A user can ''subscribe'' to viewing a meter_group (Subscription)
2011 Jan 15
3
has_many :through with Single Table inheritance
I have the following model structure setup.
class User < ActiveRecord::Base
end
class Parent < User
has_many :relationships
has_many :children,
:class_name => "Student",
:through => :relationships,
:conditions => "related_as = ''parent''"
end
class Student < User
has_many :relationships
2006 Feb 27
4
2 belongs_to to the same parent table
Hello!
I have 2 table: users and buddies
User: id, name, ...
Buddy: id, user_id, user_buddy_id, ...
So if you have 2 users 1,jack and 2,fred and fred is a buddy of jack,
there is a Buddy object: id=1, user_id=1, user_buddy_id=2
I can declare only one belongs_to in Buddy and one has_many in User. And
there is conflict if I had the second one (the first one is discarded)
class User
has_many
2006 Feb 14
10
acts_as_versioned and getting authors
Hey guys and gals,
I have the following object that has acts_as_versioned:
class Note < ActiveRecord::Base
acts_as_versioned
belongs_to :user
end
The schema for my notes table is as follows:
create_table :notes, :force => true do |t|
t.column :id, :integer
t.column :noteshare_id, :integer
t.column :user_id, :integer
t.column :title, :string
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
2011 Sep 04
2
has_many :through basic fubar
I''m trying to do, what seems like, a very basic join. However, the
association just doesn''t seem to work. I''m sure it''s something very
simple but I don''t know what.
class Role < ActiveRecord::Base
belongs_to :packages
belongs_to :users
end
class Package < ActiveRecord::Base
has_many :roles
has_many :users, :through => :roles
end
2005 Dec 29
2
has_many & belongs_to with Plugins (login_engine)
Hello list,
Probably a newbie question but couldn''t find the answer in the archives.
I am using the login_engine Plugin to implement the login functionality. I
have a JournalEntry model that interacts with the journal_entries table. I
would like to link the journal entries to the user who entered it.
Essentially, it is a one to many relationship between the model User(from
2011 Jun 11
1
Having a problem adding a foreign key
I keep getting this exception "SQLite3::SQLException: no such
column: books.user_id: SELECT "books".* FROM "books" WHERE
("books".user_id = 4)" ,which sounds like there is no user_id in books
table.So i just installed foreigner plugin and added "
t.integer :user_id, :null => false" and
"add_foreign_key(:books, :users)" in book
2006 Dec 05
1
has_many :through questions
I have a database with 4 tables
Months has_many :weeks
Weeks has_many :days
Days has_many :hours
Hours
Weeks belong_to :month
Days belong_to :week
hours belong_to :day
I need to access the month_id in the Hour.rb model.
find(:first, :conditions => ["user_id = ? AND month_id = ?", user_id,
month_id])
Thanks
--
Posted via http://www.ruby-forum.com/.
2006 Jun 08
3
Foreign key confusion
I have 2 models right now: "users" and "posts". When a user creates a
post I would like their id to be recorded under "user_id" in the "posts"
table. Under the "user" model I have:
class User < ActiveRecord::Base
has_many :posts
end
And under the "posts" model I have:
class Post < ActiveRecord::Base
belongs_to :user
2006 Apr 06
1
Primary keys and migrations
I have a join table I''m creating with a migration as follows:
create_table :read_global_messages, :id=>false do |t|
t.column :user_id, :integer
t.column :message_id, :integer
end
So far so good, however I want the primary key to be user_id and
message_id. How do I do this?
I tried the following, but that doesn''t work:
add_index
2006 Feb 26
4
Creating objects with has_many :through relationship
Three tables:
users: id
clients: id
admin_contacts: user_id, client_id
class Client
has_many :admin_contacts
has_many :contacts, :through => :admin_contacts, :foreign_key => :user_id,
:class_name => ''User''
end
I added the :contacts for ease of use of grabbing the user objects
associated with the admin_contact.
This all works great, I can grab the Users that
2011 Feb 26
1
How to manage the form of a model that has two foreign keys
Hi,
Basically I have two models: User and Godfather. The godfather table
has three columns:
- user_id (FK --> User)
- user_godfather_id (FK --> User)
- description (text)
Inside each model class, I am adding the following associations:
class User < ActiveRecord::Base
has_many :godfathers # for user_id
has_many :godfathers, :foreign_key =>
2006 Jul 25
1
save trouble with has_many belongs_to relationship
I have a bit of code in a controller that looks like this:
@current_festival.monitor_preferences.each do |pref|
if pref.user_id == @me.id
pref.preference = params[:monitor_pref]
#pref.save
end
end
@current_festival.save_with_validation(false)
the Festival class has_many :monitor_preferences
the MonitorPreference class belongs_to :festival
In the configuration above, the
2006 Mar 31
18
Modelling Foreign Keys
Can someone point me to a reference or tutorial that shows how to map
foreign key relationships in the model?
For example given:
Users
id
name
email
Posts
id
user_id
title
How do I associate user_id with users.id in the Post and User models?
has_many and belongs_to don''t seem to do it.
--
Posted via http://www.ruby-forum.com/.
2005 Dec 23
2
has_many and belongs_to relationship error
Hi i am getting following error
ActiveRecord::StatementInvalid in Blog#index
Showing app/views/blog/index.rhtml where line #10 raised:
Mysql::Error: #42S22Unknown column ''posts.user_id'' in ''where clause'':
SELECT * FROM posts WHERE (posts.user_id = 4)
Extracted source (around line #10):
7: <div class="separator"> </div>
8:
2006 Aug 13
4
has_many and foreign_key question
Alright, noob here, etc. etc.
Trying to figure out the has_many usage. If I understand correctly, when
you declare that a table/class has_many whatevers, the rails assumption
is that there will be a foreign key in the whatevers table pointing back
to the ''id'' primary key in the first table. This doesn''t seem to be the
only way to have a ''has_many''
2006 Jul 10
2
Polymorphic associations in reverse?
My app has an association that''s got me scratching my head.
I have Users, and Images. A User has many Images. (Yes, another
picture sharing app, but it''s for a small niche, and it doesn''t end
in -r!)
I would like to express a "Favorite" relationship between both Users
to Users, and Users to Images. This seems like the opposite of the
standard
2008 Mar 29
3
primary key?
can you use 2 pirmary keys.
the standard user_id key exists but i would also like to use another
key for my links. i am not sure how to implement this. also i don''t
want any auto incrementing of this second primary key that i would
like to use for my relationships.
if there is a good db, model, relationship tutorial out there i would
love to know about it.
2006 Jun 23
2
polymorphic challenge
Hello All,
I have a little challenge and I am not sure if this is even possible
I am working on a dating/event site and few functionalities are similar
that I want to centralize them.
We have users which can have
- favorite users
- interest list
- blocked users
All these relations are like;
User <-> Relation <-> User
I was thinking I could have a class called Relation (with a