user.build_book does not seem to work. I get an undefined method build_book error. However,
a= user.book.build
a.save
does work. Why would that be?
rails g migration add_user_reference_to_book user:references
class Book < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :books
end
I would like to try explain your question,
parent.build_child(attributes) this for has_one relationship (parent has_one child)
if you using has_many then you should use parent.children.build(attributes)
if user has_one book then #user.build_book will work
if user has_many books then user #user.books.build
Related
class Reminder < ApplicationRecord
has_and_belongs_to_many :offices, class_name: :failed_offices
end
class Office < ApplicationRecord
has_and_belongs_to_many :reminders
end
class OfficesReminder < ApplicationRecord
belongs_to :office
belongs_to :reminder
end
If I do this in rails console I am getting an error
p=Reminder.last
p.failed_offices
undefined method `failed_offices' for Reminder:0x00007ff87014df78
I want to rename the association for offices. So I tried failed_offices as class_name in Reminder class but it didn't do the trick. Please suggest how it is possible.
I think it might be a syntax problem.
I have a Event.rb model:
class Event < ActiveRecord::Base
belongs_to :members
has_one :brazusc
end
A Member.rb:
class Member < ActiveRecord::Base
has_many :events
end
And a Brazusc.rb model:
class Brazusc < ActiveRecord::Base
belongs_to :event
end
What I am trying to do: member.events.build_brazusc, but I get:
NoMethodError: undefined method `build_brazusc' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f99517d4a78>
How would I build an object from the has_many association?
I am also trying to retrieve the associated model from a has_many/has_one association, like this: member.events.brazusc, since brazusc has an event_id, but I get:
NoMethodError: undefined method `leads' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f99517d4a78>
How can I retrieve the object (brazusc) that is associated with event?
Any help will be much appreciated.
This would do the trick:
new_brazusc = member.events.find_by(name: "Brazusc")
new_brazusc.build_brazusc.save
so the standard way to use a has_many :through association would be to use the Physician-Appointment-Patient model from the Active Record Association Guide, right? It's basically a more verbose HABTM, with two models having a has_many :through and the connector model having two belongs_to. Now, in my current project I have a model structure like this:
class Cart < ActiveRecord::Base
has_many :line_items, through: line_item_groups
has_many :line_item_groups
end
class LineItemGroup < ActiveRecord::Base
belongs_to :cart
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :line_item_group
has_one :cart, through: line_item_group
end
Works fine. But now I want a line_item_count on Cart and can't figure out where I should add the counter_cache attribute.
Any help is appreciated :-)
First add line_item_count field in carts table, then in LineItem model add
class LineItem < ActiveRecord::Base
before_create :increment_counter
before_destroy :decrement_counter
def increment_counter
Cart.increment_counter(:line_item_count, cart.id)
end
def decrement_counter
Cart.decrement_counter(:line_item_count, cart.id)
end
end
I didn't tried it, but I think it will solve your problem.
I'm a bit new to Rails Active Record associations. I've tried to set up a relationship, but I get ActiveRecord error when I try to retrieve data. Did I associate my models incorrectly?
User has many Uploads, which has many UserGraphs:
class User < ActiveRecord::Base
has_many :uploads, through: :user_graphs
end
class Upload < ActiveRecord::Base
has_many :users, through: :user_graphs
end
class UserGraph < ActiveRecord::Base
belongs_to :user
belongs_to :upload
end
I want to get all of a user's uploads, and all of a user's graphs. The 2nd line doesn't work in rails console and gives an error
#user = User.find(1)
#uploads = #user.uploads
The error:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :user_graphs in model User
Extra Credit:
If Users have Uploads that have UserGraphs... shouldn't it be has_many :uploads and has_many :user_graphs, through :uploads?
Add
has_many :user_graphs
to the User and Upload classes.
The :through option defines a second association on top of this one.
You didn't tell Rails that you have a user_graphs association on User, only an uploads association. So when Rails goes to follow the user_graphs association on uploads, it can't find it.
So, you need add the user_graphs association. Your models should look like this:
class User < ActiveRecord::Base
has_many :user_graphs # <<< Add this!
has_many :uploads, through: :user_graphs
end
class Upload < ActiveRecord::Base
has_many :user_graphs # <<< Add this!
has_many :users, through: :user_graphs
end
class UserGraph < ActiveRecord::Base
belongs_to :user
belongs_to :upload
end
The "association?" query method that the Rails docs say should exist when I create a belongs_to association doesn't actually get created:
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
end
>> p = Post.create(:author => Author.create)
>> p.author?
NoMethodError: undefined method `author?' for #<Post:0x2555d28>
Is this a bug, are the docs wrong, or am I doing something wrong?
Take the question mark off.
p.author
It only works on boolean attributes, not belongs_to associations.