Displaying 20 results from an estimated 1824 matches for "belongs_to".
2006 Mar 23
4
belongs_to more than one model
Suppose I have one table:
states
id
statename
And I have two other tables that contains states:
houses
id
color
state_id
places
id
place_name
state_id
How would my model relationships look like?
class State < ActiveRecord::Base
belongs_to house
belongs_to place
end
class House < ActiveRecord::Base
has_one state
end
class Place < ActiveRecord::Base
has_one state
end
Is that right? Can I have a model that belongs to multiple models?
--
Vincent H Gov
-------------- next part --------------
An HTML attachment was...
2006 Mar 23
3
when (not) to use belongs_to
I know you get a lot of questions like this, but I couldn''t find one which
answers exactly what I''m after. I''m reasonably new to rails, and I''m having
a bit of trouble with when to use belongs_to. On the rails wiki (
http://wiki.rubyonrails.org/rails/pages/belongs_to), it says "In general,
the Foo model belongs_to :bar if the foo table has a bar_id foreign key
column." But according to ForumExample (
http://wiki.rubyonrails.org/rails/pages/ForumExample) doesn''t always do...
2006 Jan 12
2
Can A Model Have Many belongs_to?
I am very new to Rails. After skimming through the documentation over
the last several days, I''m struggling with the appropriate use of
belongs_to. My main question is whether a model can have several
belongs_to declarations. I have not seen any examples online show that
usage. If it can, then I would like to know if it''s poor form or what
the best practices are around it. The documentation says that the
foreign key should be...
2006 Jun 29
4
Multiple belongs_to
...''s official category.However,
there is also a category that users assign and a category that
"independent reviewers" assign. So I have this:
hotels {
id,
category_id,
reviewer_category,
user_category
}
categories {
id,
name
}
class Hotel < ActiveRecord::Base
belongs_to :hotel_class # this is fine and ok
# can I also something like this, however?
belongs_to :hotel_class,
:foreign_key => ''reviewer_category''
belongs_to :hotel_class,
:foreign_key => ''user_category''
end
I wou...
2006 Mar 19
2
Multiple polymorphic belongs_to declarations
I have the following models:
class Card < ActiveRecord::Base
belongs_to :deck
belongs_to :front, :polymorphic => true
belongs_to :back, :polymorphic => true
end
class TextContent < ActiveRecord::Base
has_one :card, :as => :front
has_one :card, :as => :back
end
The conflicting has_one declarations don''t work....
2006 Jun 22
11
Help please
...gt;
<td><%= (position.state.name) %></td>
<td><%= (position.entered_on_date) %></td>
</tr>
Here is the method in the controller:
def positionslist
@positions = (Position.find_all)
end
The model:
class Position < ActiveRecord::Base
belongs_to :category
belongs_to :contactmethod
belongs_to :edureq
belongs_to :expreq
belongs_to :postlength
belongs_to :securityclear
belongs_to :state
def self.find_all
find(:all)
end
end
Maybe something jumps out at someone ?
TIA
Stuart
2008 Jul 15
9
Beginner Question.
I''m just getting into RoR, coming from a long PHP background. So far I
LOVE IT!
I''m hitting a roadblock with updating my mysql database. For
simplicities sake, I have 2 tables and here are the relevant columns.
Table Users
id
name
email
Table Issues
id
createdby
assignedto
reportedby
In my models I have everything mapped properly I believe.
When I try to update the
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 Jun 04
6
Activerecord relations.
I''ve got two tables set up - Things and Users where Users have many
things and Things belongs to Users.
They''re connected by Things.user_id => User.id and Things.updated_by_id
=> User.id both in the Things model using Belongs_to and custom foreign
key for one of them.
Problem is when I''ve got the updated_by_id belongs_to I can''t add Things
using Things.updated_by_id = session[:user]
I get a fixnum error.
But when I remove the foreign key for updated_by_id I can''t do
things.updated_by_id.user...
2006 May 18
3
Model Madness: habtm vs through
...about habtm in rails (especially when using
acts_as_habtm_list) vs. going the :through route. Please tell me I am
thinking too hard!
My current take:
-*-*-*-*-*-*-*-*
class Person < ActiveRecord::Base
has_many :things
has_many :collections
end
class Thing < ActiveRecord::Base
belongs_to :person
has_and_belongs_to_many :collections, :order => ''position''
end
class Collection < ActiveRecord::Base
belongs_to :person
has_and_belongs_to_many :thing_collections, :order => ''position''
end
class ThingCollection < ActiveRecord::Ba...
2006 Feb 20
1
belongs_to, has_one, has_many question (again?)
...tutorial by John McCreesh.
He writes on Items and Notes (for his Todo example).
The items table has a key to the note_id. However (and this in
particular I''m trying to understand), the Models which accompany the
tables (item & note) are like:
class Item class Note
belongs_to: note # nothing here really
. .
. .
end end
It only struck me that I have the feeling that "it" (belongs_to)
should be the other way around.
I''m actually looking for some page which spills out the r...
2006 Jul 23
4
has_many AND has_many :through ?
...ations # invitations to other users'' events
has_many :events, :through => :invitations # all events the user is
invited to
#HERE IS THE PROBLEM
has_many :events # the events that the user is the owner of
end
event.rb
class Event < ActiveRecord::Base
has_many :invitations
belongs_to :user
end
invitation.rb
class Invitation < ActiveRecord::Base
belongs_to :user
belongs_to :event
end
I am not sure what is actually happening but my console tests are not
going very well. Maybe i should just go to bed but...
--
Posted via http://www.ruby-forum.com/.
2006 Jul 23
8
belongs_to :through ?
Suppose I have a belongs_to relationship and the thing it belongs to,
belongs in turn to something else.
For example, I would like to do:
belongs_to :city
belongs_to: :county, :through => :city
While it is not too much trouble to do obj.city.county, there are times
when I need to iterate through the attributes and inc...
2006 May 21
3
find with belongs_to -> belongs_to -> belongs_to
I have 3 tables
keywords (keywords attached to a file)
files (info about the file)
paths (path for the file)
[This is a legacy database, so I can''t change this]
keyword belongs_to file and file belongs_to path. Users need to be able
to search the keywords, but they only have access to certain volumes, so
the results need to be limited by the path. I also need to show paging,
like "Results 11 - 20 of 315", and possible sort the results.
I guess I can do a search...
2006 Nov 09
2
redefining the method belongs_to in ActiveRecord::Base
Hi. I''ve tried to redefine the method belongs_to in ActiveRecord::Base,
so every time a model is defined as belonging to :attachment, it should
have some additional methods. I tried to do an alias of belongs_to, but
this was not successful. The error message in Webrick was
> Booting WEBrick...
./script/../config/../lib/ActiveRecordExtensions....
2006 Apr 02
7
RANT: belongs_to -> refers_to
Every once in a while, I pipe up and whine that "belongs_to" should really
be ''refers_to" [http://dev.rubyonrails.org/ticket/2130]. It''s time again.
I''ve got a bunch of stuff going onto eBay, so, like any good engineer, I
don''t just post it manually: I design a Rails-based inventory database that
creates a sem...
2008 Apr 29
4
Default values for belongs_to and has_one
There really doesn''t seem to be a *declarative* way to define a default
value for belongs_to and has_one associations
class Person < ActiveRecord::Base
belongs_to :address
end
p = Person.new
p.address.city = ''Atlantis'' # Bummer!
Of course, there''s an easy way to avoid this
class Person < ActiveRecord::Base
belongs_to :address
def...
2006 Mar 29
11
why belongs_to does not like validation?
This works :
class Recipe < ActiveRecord::Base
belongs_to :category
end
But (when I add validation) this does not work :
class Recipe < ActiveRecord::Base
belongs_to :category
validates_length_of :category, :within => 6..20
validates_uniqueness_of :category, :message => "already exists"
end
thank you
--
View this...
2006 Feb 17
3
Using :include with has_one vs. has_many
...than 1 step
away from the parent object, but I kind of understand why it can''t. Now, on to
my "discovery"...
Completely contrived example follows:
I have the following Models:
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
has_one :category
end
class Category < ActiveRecord::Base
belongs_to :post
end
There are many authors in the database that no posts, and there are several
posts in the database that have no category.
If I do:
@authors = Author.find(:all, :include => :posts)
Every entry...
2006 Jan 31
4
has_one without inverse belongs_to
I have two models called entity and user.
The entities table has a column called users_id that contains the user
id of the user that created the entity.
In entity I have...
has_one :user
... as I want to be able to show the user who created the entity from
the entity object.
But this produces the following error...
Mysql::Error: #42S22Unknown column ''users.entity_id'' in