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
Related
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
My instance method of active_posts is not working on my User model.
I am grabbing a user record, then for that record I need to join the blogs and posts table, and then filter by the post's active attribute in order to return only the active posts for that user.
class User < ApplicationRecord
has_many :blogs
has_many :posts, through: :blogs
def active_posts
joins(blogs: :posts).merge(Post.active).distinct
end
end
class Blog < ApplicationRecord
belongs_to :user
has_many :posts
end
class Post < ApplicationRecord
belongs_to :blog
belongs_to :user, through: :blog
scope :active, -> {where(active: true}
end
I run the following:
User.first.active_posts
And here is the error:
NoMethodError
Undefined method 'joins' on the user record
Just realized my error.
Just call posts.active within the active_posts method.
def active_posts
posts.active
end
joins is used for a collection. So you would use joins on a class method in your model, not within an instance method. Simple error.
I am having issues setting up model relations in Rails.
I have a User. A user can have many requests. A request can have one response. I set up my models like this:
Class User < ActiveRecord::Base
has_many :user_requests
has_many :request_responses, through: :user_requests
end
Class UserRequest < ActiveRecord::Base
belongs_to :user
has_one :request_response
end
Class RequestResponse < ActiveRecord::Base
belongs_to :user_request
end
Whenever I try to do something like:
UserRequest.request_response.id
I get errors that say either the relationship doesn't exist or the column does not exist in the table. Have I set up my relationships incorrectly?
You will get error:
UserRequest.request_response.id
Because:
request_response is expected to be a class method of UserRequest.
Association is defined as request_responses, not request_response, so calling user. request_response won't work either.
What to do?
call user.request_response_ids where user = User.first.
I'm having problems fixing association that is working on a rails 2 application. I'm this error on my local machine:
undefined method `quoted_table_name' for Enrollment:Module
I can't seem to figure out where the error is.
Model:
class Enrollment < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :enrollment
accepts_nested_attributes_for :enrollment
end
Controller:
class UsersController < ApplicationController
def new
#user = User.new
#user.build_enrollment
end
end
UPDATE:
Fixed the problem because the name of the model is the same as the name of the rails application.
Did you run rake db:migrate to make sure the tables exist
Make the following change since the nested attributes clause is misplaced
class Enrollment < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :enrollment
# This line belongs here right?
accepts_nested_attributes_for :enrollment
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.