Displaying 20 results from an estimated 11000 matches similar to: "Reading and saving data with belongs_to and has_many"
2006 Mar 22
2
habtm vs. has_many/belongs_to
In AWDWR David says (pg 232),
"When a Join Wants to Be a Model:
While a many-to-many relation with attributes can often seem like the
obvious choice, it?s often a mirage for a missing domain model. When
it is, it can be advantageous to convert this relationship into a
real model
and decorate it with a richer set of behavior. This lets you
accompany the
data with methods.
As an example,
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
2013 Nov 10
3
accepts_nested_attributes_for how, example
I have following tables with following models:
users(id, role_id) *has_many :entries*
categories(id, category_name) *has_many :entries*
entries(id, category_id, user_id) *belongs_to :user, belongs_to
:category, has_one :storage*
storages(id, title, content, entry_id)* belongs_to :entry*, *has_one
:vote*
votes(id, count, storage_id) *belongs_to :storage*
Now,
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
2006 Feb 20
1
belongs_to, has_one, has_many question (again?)
Hi All,
Probably this has been asked numerous times, I apologize already!
Can somebody point me to a good tutorial on how Rails works with
relations?
I know about database design and normalization, I also know about
programming in general (and OOP for that matter).
Only thing I can say is that I''m following the "Four days on Rails"
tutorial by John McCreesh.
He writes
2006 Jun 11
0
Reaching through a belongs_to for acts_as_list scope?
I''m running into a situation where I''d like to provide the list scope
for an acts_as_list model from one step removed. In minimalistic form
(with several other associations removed from each level).
class MainEntity < AcrtiveRecord::Base
has_many :containers
end
class Container < ActiveRecord::Base
has_many :elements
belongs_to :main_entity
end
class
2007 Jul 05
0
act_as_tree, belongs_to and has_many foreign keys incorrect
Two models:
class Project << ActiveRecord::Base
has_many :files
end
class File << ActiveRecord::Base
has_one :project
acts_as_tree
end
Inside of the project model, when creating a new product, I want to
store the hierarchy of files within it.
If I do,
my_top_level_file = my_project_object.files.create(:file_name =>
''foo'');
2007 May 14
0
building and saving has_many and has_one relations
Hi,
I am very confused about an aspect of has_one and has_many relations,
with regard to using the build method, and saving the belongs_to side.
It''s a somewhat long post. so please bear with me :)
First let''s consider the has_one scenario. Let''s say I have a student
who must own exactly one car:
class Student
has_one(:car)
end
class Car
belongs_to(:student)
2006 May 31
0
belongs_to without referencing an id as a foreign key?
Hello everyone,
Here''s my problem for the day: I have three tables that look a little like
the following
table_a
id
table_b
id
position
table_a_id
name
table_c
id
table_a_id
table_b_position
There will be many rows in table_c that have the same table_a_id, and the
same table_b_position. These will all be referencing the same name value in
table_b. The row in table_b may
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 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
2008 Jul 29
0
cannot destroy child records
Hi, i am lost here,
i have these models:
class Shift < ActiveRecord::Base
has_many :transactions,:dependent => :destroy
has_one :exchangeproposal
class Transaction < ActiveRecord::Base
belongs_to :shift
has_many :exchangeproposals
class Exchangeproposal < ActiveRecord::Base
belongs_to :shift
belongs_to :transaction
with these models i can''t destroy an
2006 Jul 11
1
counter_cache, has_many and belongs_to
I am a bit confused about counter_cache here. The API docs
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
say that only the belongs_to association can take the :counter_cache
option. When I try to use it on a has_many I get an "unknown key(s):
counter_cache" error.
Why would this be the case? belongs_to means that this model has a
field with an id
2006 Aug 01
2
has_many :through a belongs_to relationship
I have users who belong to a group. I also have a room that belongs to
a group. I want to use a has_many through association to link the room
and the users as such:
class Room < ActiveRecord::Base
belongs_to :group
has_many :users, :through => :group
end
The SQL generated for the search is incorrect:
SELECT users.* FROM users INNER JOIN group ON users.group_id =
groups.id WHERE
2006 Dec 01
0
How to save has_one belongs_to models?
How can I save two models that have has_one and belongs_to
relationship to each other? I want to save both the models by using
script/console. TIA.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to
2006 Jan 20
1
stack level too deep from has_many / belongs_to relationship
I have the following 3 models (2 models joined by an intermediate):
1. class Exercise < ActiveRecord::Base
2. has_many :routines, :class_name=>''RoutineExercise''
3. end
1. class Routine < ActiveRecord::Base
2. has_many :exercises,
3. :class_name=>''RoutineExercise'',
4. :order=>''position''
5. end
1. class
2006 Jul 31
0
Issues with has_many, belongs_to and dependency settings
I have 2 models SmsAddress & OutboundSmsAtom
SmsAddress {
has_many :other_objects, :dependency => :nullify
}
OutboundSmsAtom {
#note this object also "belongs_to" other objects if that matters
belongs_to :this_object
}
Whenever I try to destroy an object of type ThisObject, which at the time
has 0 "children" OtherObjects I get the following type of
2006 Jul 31
0
Polymorphic has_many association through a belongs_to
Hi,
Let''s say I''ve the following Models and their relative associations:
Seat
----
belongs_to :booking
Booking
-------
has_many :seats
has_many :debits, :as => :chargeable
Debits
------
belongs_to :chargeable, :polymorphic => true
Why
>> seat.booking.debits
nil
while
>> Booking.find(seat.booking_id).debits
[]
?
Thanks.
Cheers,
Marco
2006 Apr 16
1
has_many :through and belongs_to
I have a relationship where a Show has_many Techs through People. From
what I understand that requires me to put belongs_to Show in the People
model. My problem is that a Tech can belong to many Shows. Is there
any way of solving this besides putting a has_many Shows relationship in
People
--
Posted via http://www.ruby-forum.com/.
2009 Jan 22
0
ActiveLdap belongs_to and has_many associations
Hi,
I am trying to use ActiveLdap for authentication in my rails app. But I
am running into a problem with the "belongs_to" and "has_many"
associations.
Here ist how my models look like.
class Person < ActiveLdap::Base
ldap_mapping :dn_attribute => "uid",
:prefix => "ou=People",
:classes =>