Displaying 20 results from an estimated 5000 matches similar to: "belongs_to more than one model"
2006 Jan 31
2
Relationship navigation issue
class Event < ActiveRecord::Base
belongs_to :venue
belongs_to :category
end
class Venue < ActiveRecord::Base
belongs_to :city
end
class City < ActiveRecord::Base
belongs_to :state
end
I want to retrieve all event records belonging to a state. Coming from
hibernate background I tried something like this:
def self.list_for_a_state(state_id)
find_by_sql(["select e.*
2006 Jul 14
20
Method for associated relationships
I have these tables set up like this:
listings has_many :states
<field>state_id [int]
<other fields....
..............
.............>
states belongs_to: listings
<field>name
<other fields..
...............
.................>
In my view I have <%= listing.name %>
have also tried listing.state_id.name , that didn''t seem to do the magic
either.
This
2012 May 28
4
How to load a selection list into the method new of a controller?
Hi friends!
I''m relatively new with Rails and I''m struggling for a long time with this
problem (it should have a pattern solution but until now I didn''t find it):
I have the following models: Institution, City, State and Country.
class Country < ActiveRecord::Base
has_many :states
has_many :cities, :through => :states
end
# == Schema Information
# Table
2011 Jul 11
36
has_many and belongs_to association
Hi ,
I want to test the one below but I got the problem
belongs_to :name, :class_name => "Phrase", :foreign_key => "name"
in my test
context "test"do
should have_many :phrases
end
in language.rb
belongs_to :name, :class_name => "Phrase", :foreign_key => "name"
error is
1) Failure:
test: check has_many and belongs_to
2006 Jun 03
8
confused about ActiveRecord relationships
I am very confused about where to put the belongs_to and the has_one
(and other relationship identifiers). I have read the RDoc and the
agile book many times about this and I think i still see it backwards.
Let me outline my app so you have an understanding...
I have 2 tables:
Schools { id, school_name, address_id }
and
Addresses { street1, street2, city, state, zip, country }
*** this
2006 May 28
3
Validating Foreign Key
Howdy,
I''m a Rails beginner working on my first significant project with RoR.
I''m trying to figure out how to validate a foreign key (you know, make
sure the row actually exists.)
However, validates_associated doesn''t seem to be what I want to use, nor
does it work...
-- BEGIN --
class City < ActiveRecord::Base
has_many :schools
belongs_to :state
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
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 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. What I need is
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
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 semantically-correct XHTML/CSS auction
2006 Oct 25
12
NameError, uninitialized constant States
I''m completely new to Ruby and Rails and could use some help resolving
an issue. I have a list method that is supposed to show the titles and
states(status) of requests, but I get an error from this piece of code:
<td><%= change.states.state %></td>
The error is:
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:100:in
2006 Apr 07
5
Confusion about has_many / belongs_to ...
I have a table called players and its model Player, and I have a table
called games and its model Game.
In the games table I have:
player1_id int(11)
player2_id int(11)
I want the tables to be associated so that I can access the player
objects using table.player1 and table.player2 rather than having to
lookup the objects based on the ids.
I am not sure how to go about this but I suspect I
2006 May 24
5
where can i find a good tutorial for has_one and belongs_to?
thanks for helps!
--
Posted via http://www.ruby-forum.com/.
2011 Sep 16
4
belongs_to not working as
Hi,
I have a model tests_user and score which are associated with each other by
has_one and belongs_to association.
class TestsUser < ActiveRecord::Base
has_one :score, :foreign_key => :reg_no
end
class TestsUser < ActiveRecord::Base
belongs_to :tests_user, :foreign_key => :reg_no
end
where reg_no is a unique field in both the table!
score = Score.find(:first) gives me
2005 Dec 14
5
belongs_to and multiple foreign keys
Hi all,
I''m confused about how I''m supposed to specify a belongs_to relationship when
the table includes multiple columns that reference the same parent table.
In my case I have a "schedules" table with "opened_by" and "closed_by" columns,
that both reference the "users" table. In theory, they can be two different
user ids.
table
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
2010 Aug 10
4
(Dreaded) STI, belongs_to
Having problems with STI. Names below have been changed to protect the
innocent. Imagine a system that allows someone to apply for one of two
different types of school scholarships. Each scholarship goes through a
different review process, represented by a state machine (this is not a
state machine question). So there are two state machine classes that
differ slightly and subclass a generic
2006 Jan 11
6
How to display the error messages from the controller
I have a wizard which consists of two pages. To keep it simple let''s say
that the first page asks the user to select a state from the drop down menu.
In case the user doesn''t select any state, an error message needs to be
displayed.
Ideally I would like to do something like this.
def new
selected_value = params[:addevent][:state_id]
if selected_value == "-1"
2010 May 11
1
has_one/belongs_to -- accessing the subordinate
With a has_one/belongs_to relationship, what''s the best way to guarantee
that the belongs_to object gets created and is accessible alongside the
has_one object? I *think* the after_create callback is a good choice,
but I discovered an oddity while trying it.
F''rinstance, if every horse has a carriage:
============
ActiveRecord::Schema.define do
create_table(:horses) {|t|