I have these three model:
class Project < ActiveRecord::Base
has_many :garden
end
class Garden < ActiveRecord::Base
belongs_to :project
has_one :garden_customer
validates_uniqueness_of :idjardin, :message => "Este codigo jardin ya esta utilizado"
end
class GardenCustomer < ActiveRecord::Base
belongs_to :garden
end
If I run project=Project.find(1) and then garden=project.garden.find(1) everything is ok. But when i try to get the garden customers I got a nil object instead of an empty array or values, that is:
garden.garden_customer
is nil.
I can't call any method on garden.garden_customer.
Example of code:
#project=Project.find(params[:project_id])
#garden = #project.garden.find(params[:garden_id])
#garden_customers = #garden.garden_customer.all
I got the error "undefined method `all' for nil:NilClass"
Since Gardenis a has_many, it should really be setup like has_many :gardens
Edit: Also garden_customer is setup as a has_onerelationships, so there is only 1 garden_customer per garden and so garden.garden_customer.all is wrong. garden.garden_customer itself would directly give you the associated garden_customer provided it was setup during creation.
Related
Trying to build a friends feature on top of a Solidus framework, but the .friends method does not work. It does in rails console, however.
SpreeUsers Controller (current_spree_user.friends causes error):
class SpreeUsersController < ApplicationController
def my_friends
#friendships = current_spree_user.friends
end
def search
#spree_users = SpreeUser.search(params[:search_param])
render json: #spree_users
end
end
Friendship Model:
class Friendship < ActiveRecord::Base
belongs_to :spree_user
belongs_to :friend, :class_name => 'SpreeUser'
end
SpreeUser Model:
class SpreeUser < ActiveRecord::Base
has_many :friendships
has_many :friends, through: :friendships
end
Error:
undefined method `friends' for # Did you mean? friendly_id?
https://i.stack.imgur.com/u4F8K.png
Console Input/Output:
https://i.stack.imgur.com/YuVjb.png
I believe I know what is happening here.
You have declared a model SpreeUser with a friends relationship. This is why in your console it works. You are correctly calling the friends method on the class SpreeUser.
The error you are receiving is for the class Spree::User (Note the different class names). I am assuming you are using spree_auth_devise which provides that class.
You will need to properly add your logic from SpreeUser to Spree::User. I believe Spree/Solidus recommends doing so using decorators.
EG
Spree::User.class_eval do
has_many :friendships
has_many :friends, through: :friendships
end
Above not tested and a best guess taken from this SO answer
You could also test my theory by running Spree::User.first.friends in your console. You should receive a similar error to the browser.
I'm getting the strangest error I've seen in Rails so far. In my view, I can print out the email associated with a painting if I find the record directly (e.g. Painting.find(15). But if I try to use an instance variable it errors (e.g #painting).
views/paintings/show.html.erb
<%= Painting.find(15).artist.user.email %> # works
<%= #painting.artist.user.email %> # Error: "undefined method 'user' for nil:NilClass"
controllers/paintings_controller.rb
def show
#painting = Painting.find(15)
end
Models: "users", "artists", "paintings".
A user can be an artist. So a user has_one artist.
An artist has_many paintings.
I think you should add associations. This how they should look like:
class User < ActiveRecord::Base
has_one :artist # it's ok
end
class Artist < ActiveRecord::Base
belongs_to :user # add this
has_many :paintings
end
class Painting < ActiveRecord::Base
belongs_to :artist
end
For me both cases works with this associations.
Use
def show
#painting = Painting.find(15).last
end
Currently the second one is returning a 1 element array, but in order to call a dependent method, you must specify 1 item.
I've got the following two classes
class Car < Vehicle
has_one :steering_wheel, as: :attached
end
class SteeringWheel < ActiveRecord::Base
belongs_to :attached
has_many :components
has_one :rim, class_name: 'Components', order: 'id DESC'
attr_accessible :components
end
class Component < ActiveRecord::Base
include SpecificationFileService::Client
attr_accessible :created_by
belongs_to :steering_wheel
end
Then in my specs:
context "given an attachment", :js do
before do
#car = create(:car, make: "honda")
#steering_wheel = SteeringWheel.create(attached: #car)
#steering_wheel.save
#car.save
#car.reload
end
specify "test the setup", :js do
puts #car.steering_wheel
end
end
Which prints: nil
A way that I have found fixes this is explicitly setting steering_wheel on car like so:
#car.steering_wheel = #steering_wheel
just before the save.
EDIT:
As suggested in the comments below, I have tried adding polymorphic: true, which did not resolve the issue. Also, I've fleshed out more of the SteeringWheel model above
My question is why, and how can I add this to the callback chain implicitly
Like #abraham-p mentioned in a comment, you need to declare the belongs_to relation as:
belongs_to :attached, polymorphic: true
Otherwise it will attempt to look for an Attached model, and be sure to include these fields in your SteeringWheel model:
attached_type
attached_id
The rest is worked out by Rails :)
I've been bashing my head against a wall for a while on this one and I can't get it to work. I have three models:
class Instrument < ActiveRecord::Base
has_many :analytical_methods
has_many :analytes, :through => :analytical_methods
accepts_nested_attributes_for :analytical_methods
attr_accessible :name, :analytical_methods_attributes
end
class AnalyticalMethod < ActiveRecord::Base
belongs_to :instrument
has_many :analytes
accepts_nested_attributes_for :analytes
attr_accessible :name, :analytes_attributes
end
class Analyte < ActiveRecord::Base
belongs_to :analytical_method
attr_accessible :name
end
And I have the following factories:
Factory.define :analyte do |analyte|
analyte.name "Test analyte"
end
Factory.define :analytical_method do |analytical_method|
analytical_method.name "Test method"
analytical_method.association :analyte
end
Factory.define :instrument do |instrument|
instrument.name "Test instrument"
instrument.association :analytical_method
instrument.association :analyte
end
Any time I try to Factory(:instrument) or Factory(:analytical_method), it throws the following error:
NoMethodError:
undefined method `analyte=' for #<AnalyticalMethod:0x00000104c44758>
Am I missing some ridiculous typo or something? The website works perfectly fine, but the tests keep failing. Thanks for any help in returning my sanity!
I believe it's because you're using instrument.association :analyte and analytical_method.association :analyte for a has_many relationship. The association declaration is used for belongs_to relationships.
I typically don't use Factory Girl to create has_many relationships, but if you choose to go this route, you're not the first person to do so. Here's a blog post that's a few years old, but seems to describe what you're trying to do.
rails 3 newbie, using Devise for auth...
I want to create the following models:
class Instance < ActiveRecord::Base
has_many :users
has_many :notes
end
class User < ActiveRecord::Base
belongs_to :instance
end
class Note < ActiveRecord::Base
belongs_to :instance
end
To create a new note in the notes_controller.rb
def create
#note = instance.notes.build(params[:note].merge(:instance_id => current_user.instance_id))
end
But I'm getting the following ERROR: "undefined local variable or method `instance' for #"
Ideas?
You haven't assigned anything to "instance" yet, so there's nothing to reference. If you know the instance record already exists in the database, you could do something like:
#instance = current_user.instance
#note = Note.create(:instace_id => #instance.id)
If not, you'd need to check and create it first if necessary, using the same kind of syntax.