Displaying 20 results from an estimated 2000 matches similar to: "ActiveRecord associations: design question"
2013 Jan 03
6
How to avoid simple race conditions with Session?
Here is the situation:
- the client makes a request
- it is followed by AJAX polling
The server needs to keep track of some stuff during this process.
I have been using the Session and it "mostly" works… until the user
decides to open another tab in her browser and she makes a new,
parallel request. In this case, the data from the two requests is
merged, which leads to bugs.
This got
2009 Oct 04
3
Problem using ActiveRecord find with :joins and :select
Hi guys,
When I do:
temp = User.find(:all,
:joins => "INNER JOIN contents ON users.agent_id =
contents.id",
:select => "contents.id, users.id, users.u_date")
temp.first.inspect
It seems to work fine, but it only returns:
#<User id: 6, u_date: "2009-10-03 19:32:23">
but not the contents.id information.
I
2012 Apr 02
10
Pessimistic locking locks the entire table?
Hi guys,
I must be missing something obvious with pessimistic locking. Here is
what I do (Rails 2.3, mySQL):
In my Foo model:
def test
Foo.transaction do
lock = UpdateLockForFoo.find_by_foo_id(self.id, :lock => true)
sleep 30
end
end
For each Foo model, I have an UpdateLockForFoo. This "lock" needs to
be acquired before doing any changes to Foo.
I believe it works,
2010 Jan 10
3
Need advice: threads or Typhoeus?
Hi guys,
I need my app to make a few hundreds requests to external web services
as quickly as possible. The web services are fairly fast, but still:
if I do the requests one after the other, it is too slow.
My understanding is I have two options:
a- Threads.
b- Typhoeus
Typhoeus seems great. Yet, the web services are all quite different
(some are SOAP, some REST...) and therefore wrapped into
2006 Apr 20
4
Question about Associations
Hi all. Got a stupid-simple question about associations. I have two
models - school and course. There are a fixed number of schools (set
up in the migration). Each course is assigned a school and a school
will be associated with multiple courses...
How do I set up the associations? Do (can) I have School :has_one
:course and Course :has_many :schools?
Does the schools table then get a course_id
2012 Dec 24
4
Bad idea to add/remove associations in after_find
I have 2 distinct types of users (artists and curators) in my system and
they currently share all associations (eg has_one :portfolio).
However, a curator shouldn''t have a portfolio so I would like to only
add that association when required. Obviously I could just return nil
for that method, but it doesn''t feel right having that there in the
first place.
What''s the best
2006 Mar 24
7
Polymorphic associations?
I''ve read the stuff about polymorphic associations here:
http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations
But I''m not sure what exactly they are and what their advantage is. Are
they the same as HABTM, but they''re "two-way"?
Joe
--
Posted via http://www.ruby-forum.com/.
2008 Sep 16
3
has_one :through eager-loading problem
I have a problem with the :include option in find for my
has_one :through association.
I have Users, Profiles and Customers, all connected to each other with
a has_one :through => :membership association like this:
class Profile < ActiveRecord::Base #could also be user or customer
has_one :membership # i saw an acrticle of Ryan about
has_one :through, there this would be has_many, is
2010 Apr 15
12
[PATCH] Add :touch option to has_many associations
Hi,
it would be nice to have the touch option in has_many associations also.
I have a scenario like this:
An user has some credit (money) and some advertises. Each advertise can be
enabled or disabled depending on some rules. One of the rules is that the
user should have money. Each time the money of the user change, the
advertises are touched, enabled/disabled and then cached, as they are much
2006 Jul 11
7
Rails associations: Easy Question requires dumb answer
***Scroll down to see the dumb quick question***
I have two Models:
class Template < ActiveRecord::Base
end
which has this migration:
/snip
t.column :name, :string
And the other model:
class Product < ActiveRecord::Base
has_one :template
end
with the migration:
/snip
t.column :qty, :integer, :default => 0
t.column :specialDescription, :string
t.column
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 <
2005 Dec 15
8
How to ensure deletes cascade to associated models?
I have the following models:
class Resume
belongs_to :resume_assignments
end
class User
belongs_to :resume_assignments
end
class ResumeAssignment
has_one :resume
has_one :user
end
When I delete a resume, I want to make sure that its associated
ResumeAssignment gets deleted also. Calling destroy_all to delete
some resumes doesn''t seem to do this. Is there an option that will
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
2010 Nov 28
6
has_one accepts_nested_attributes_for fields_for NOT WORKING HELP
MODEL
class User < ActiveRecord::Base
has_one :address, :dependent => :destroy
accepts_nested_attributes_for :address
end
CONTROL
def new
@user = User.new
@user.build_address # Adicionei
...
VIEW partial _form
....
<% f.fields_for :address do |b| %> # Adicionei
<%= b.text_field :city_manual %> # Adicionei
<% end %>
2006 Jul 30
3
ActiveRecord - Multiple Address for a single record
I''m trying to figure out the Rails way to model the following problem:
I have 2 tables. One (let''s call it location) contains a single
address. The other (let''s call it company) contains 3 address. I''m
trying to figure out the best way to model this. I''ve created an
address table, a set up the model as follows:
class 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
2007 Oct 18
4
Polymorphic Association?
Hi there-
I''m a newbie to this board, RoR, and web development in general. So
please forgive me if this is in the wrong place or if the solution is
really simple...
I''m trying to conceptualize the relationships between a series of
models for a page creation function. What I''d like is to have a
Content Page which consists of a series of Content Blocks. The
Content
2006 May 12
1
can I fake has_one :through?
I''m having a difficult time wrapping my head around a set of models
and their associations and seeing how they map into Rails. I''ll try
to lay out my problem using the has_many and belongs_to structures. I
have a RuleSpace, a Rule, and a Subject.
A RuleSpace has_many Rules
A RulesSpace has_many Subjects through Rules
A Rule belongs_to (many) RuleSpaces
A Rule has_many
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 Feb 26
3
Rails Naming Conventions
DB field names:
If I have a table that references 2 or more separate users from my users
table, is there a recommended naming convention for this situation? In
my case, I have 3 users associated with a record in my projects table:
requester_user_id
created_by
updated_by
I could name one of them "user_id", and then projects.user would work I
guess, but wouldn''t work