Displaying 20 results from an estimated 20000 matches similar to: "Adding attributes to many through associations"
2006 Apr 05
0
Has Many :through associations
Greetings again:
I''m simplifying the case I presented earlier regarding the use of
has_many :through. I''m using DHH''s example from "Persuing Beauty with
Ruby On Rails".
Here are his models:
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end
class Book < ActiveRecord::Base
has_many
2010 Feb 26
0
save has-many associations
I have the following association
class Document < ActiveRecord::Base
has_many :sections
has_many :items, :through => :sections
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :section
end
Here is the sample view for the ''new'' action
<% form_for(@document) do |f| %>
<%=
2006 Mar 24
2
Change to has_many :through associations
Hi everyone. I''ve made a default change to has_many :through
associations that I felt was important to make before they''re
released. This is after some tickets and confusion I''ve seen
regarding has_many :through.
class Connection < ActiveRecord::Base
belongs_to :user
belongs_to :channel
end
class Channel < ActiveRecord::Base
has_many :connections
2006 Apr 12
0
Looking for a simpler model using :through associations
Hi. I''m building a setup where I have users and groups, and a user can
have an optional title in a group (eg. "owner"). I''m doing this using
"has_many :through" associations, using a Role model object as the join hub:
class User < ActiveRecord::Base
has_many :roles, :dependent => :destroy
has_many :groups, :through => :roles
end
class
2006 Jun 13
0
question about saving associations when using has_many :through
I''ve got a User model and a Book model. A user is required to submit
books each month to be reviewed by other users. I have the following
relationships defined:
# the join model
book_review.rb
belongs_to :user
belongs_to :book
end
book.rb
# each book is reviewed by many users. This allows us to get the BookReview
# objects associated with this book
has_many :book_reviews,
2006 Apr 04
1
Manipulating has_many :through associations
Hi,
I haven''t done any associations before, so please forgive my incorrect
use of terminology...
If I set up a has_many :through association, can I manipulate the
associated instances directly (like an Array), or do I need to
manipulate the join model?
For example, suppose I have two classes and the associated join model:
class foo
has_many :foo_bars
has_many :bars, :through
2006 Aug 16
1
Naming rights_roles join model using has_many :through and polymorphic associations
Hi.
I have a couple of best practices questions regarding polymorphic
associations, naming join tables and user permissions.
Currently I have implemented the user authentication model from the
rails recipes book. Basically it goes something like this:
MODEL CLASSES:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
end
class Role < ActiveRecord::Base
2006 Aug 16
0
Polymorphic Associations and grouping users and projects
Hi all,
In my app I would like to create user groups in my admin. Currently I
have the following models and associations that facilitate this.
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
end
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
class Group < ActiveRecord::Base
has_many
2006 Jan 15
2
new objects and new associations in same view
Hello All,
I generated scaffolding for my Users table and setup my additional
models with their associations. I want to be able to create a new user,
and its associated groups and privileges from the same view. My
relationships are as follows:
users:
has_and_belongs_to_many :groups
has_many :privileges
groups:
has_and_belongs_to_many :users
privileges:
belongs_to :user
Right
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
2011 Jul 26
1
ActiveRecord has_many associations
Given the models Country, State, City and Person as follows.
class Country < ActiveRecord::Base
has_many :states
end
class State < ActiveRecord::Base
belongs_to :country
has_many :cities
end
class City < ActiveRecord::Base
belongs_to :state
has_many :people
end
class Person < ActiveRecord::Base
belongs_to :city
end
Is there any way that doesn''t allow to delete
2006 May 12
2
Has many through join table issues
I''m trying to wrap my caffeine soaked brain around has_many :through
following along at:
http://rails.techno-weenie.net/tip/2005/12/23/teaching_your_blog_model_new_tricks_with_has_many_through
I think my models are a little more complex than what fits this
narrative.
I have a directory of members, each member can belong to multiple
categories.
The category table references itself
2006 Jul 27
0
CRUD, REST and associations
Let''s say I have the model class Reader and Magazine, connected by join
model Subscription. It looks something like this
class Reader < ActiveRecord::Base
has_many :subscriptions, :dependent => :delete_all
has_many :magazines, :through => :subscriptions
validates_presence_of :name
end
class Magazine < ActiveRecord::Base
has_many :subscriptions, :dependent =>
2013 Mar 30
1
How to use group in nested associations
The below query fails:
Timesheet.joins(:time_entries).select("timesheets.*,
sum(time_entries.worktime) as total").group("timesheets.start_date")
The models have the following relations:
Timesheet < AR
has_many :activities, dependent: :destroy, inverse_of: :timesheet
has_many :time_entries, through: :activities
accepts_nested_attributes_for :activities,
2006 Apr 08
4
saving related objects using has_many_through associations
This only saves the @person but not the @address. Shouldn''t it save
both?
@person = Person.new(params[:person])
@address = Address.new(params[:address])
@person.address << @address
@person.save
here is what I have:
class Person < ActiveRecord::Base
has_many :addressables
has_many :addresses, :through => :addressables
end
class Address <
2006 Jun 14
0
testing table associations
First, thanks to everyone who replied to my pluralization question
from before! Today I''m confused by associations. I''ve set up a bunch
of associations in my models to describe these relationships:
- each user can have many bookmarks and tags
- each bookmark can have many users and tags
- each tag can have many bookmarks and users
(Part of what is confusing is that I can
2006 May 24
2
Has Many Through + Join Model + Forms + Confused?
Hello,
I''m trying to implement something like the following scenario. I''ve got
"Alloys" (blends of metals), "Metals" and "Percentages" I want to implement
this using a join model, not using a has_and_belongs_to_many relationship.
(If i''m wrong about that, let me know).
Basically, i have the following tables:
Metals
id
name
Alloys
2007 Dec 07
0
Associations counting
Hi,
I''m using the models, User, Item, and Add.
Add belongs_to User (with counter_cache enabled)
Add belongs_to Item (also, with counter_cache enabled)
User has_many adds, and has_many items through adds
Item has_many adds, and has_many users through adds
When I go to save a new Item, I want to create a new Add for this, with the proper (loged in) User:
@item.adds.create :user =>
2006 Feb 03
1
nested has_many associations
My question is whether the rails helper methods for handling associations can
be nested (see examples below). I suspect not as currently I''m
getting "unknown method" if I try and use two associations in one call.
The essence of the code is
class User << ActiveRecord::Base
has_many :items
class Items << ActiveRecord::Base
has_many :reservations
belongs_to
2006 Apr 04
4
Help with many-to-many using :through
I am using many-to-many using :through since I need to store additional
information in the join table.
I am doing this and it seems to work. I want to verify that this is the best
way to do this.
Tables
foods - id, food
foodallergies - food_id, symptom_id, a few other columns
symptoms - id, symptom
Models
class Food < ActiveRecord::Base
has_many :foodallergies
has_many :symptoms,