I have installed Merit and have set up some points_rules. I'm successfully adding points to my 'user' when they create a comment but none of the others.
I am using Devise.
point_rules
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 10, :on => ['user#create', 'user#update'] do |user|
user.preferred_drink.present?
end
score 20, on: 'comments#create', to: :user #is working!
score 10, on: 'users#update', to: :user
score 20, on: 'favorite_coffeeshops#create', to: :user
end
end
end
user.rb
class User < ApplicationRecord
has_merit
has_many :favorite_coffeeshops
has_many :favorites, through: :favorite_coffeeshops, source: :coffeeshop
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :trackable
favourite_coffeeshop.rb - joins table.
class FavoriteCoffeeshop < ApplicationRecord
belongs_to :coffeeshop
belongs_to :user
The user is able to add a new row in the favorite_coffeeshops table perfectly fine so that side is working ok.
From the console log it doesn't appear to be trying to write to the merit models.
Started PUT "/coffeeshops/new-shop-9c762d25-77e4-499a-b009-1ad150515dbb/favorite?type=favorite" for 127.0.0.1 at 2019-01-13 21:57:08 +0000
Processing by CoffeeshopsController#favorite as HTML
Parameters: {"authenticity_token"=>"24cQlLc+UYT79P50rrBx9hlXckPj/3nCtVJ7fS6+UKO+UHTGDLBXlwLaVpVLf6Yj46VYyNiH0xdBzaUaU/LbHw==", "type"=>"favorite", "id"=>"new-shop-9c762d25-77e4-499a-b009-1ad150515dbb"}
Admin Load (0.8ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = $1 ORDER BY "admins"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Coffeeshop Load (0.5ms) SELECT "coffeeshops".* FROM "coffeeshops" WHERE "coffeeshops"."slug" = $1 LIMIT $2 [["slug", "new-shop-9c762d25-77e4-499a-b009-1ad150515dbb"], ["LIMIT", 1]]
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.3ms) BEGIN
FavoriteCoffeeshop Create (0.9ms) INSERT INTO "favorite_coffeeshops" ("coffeeshop_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["coffeeshop_id", 22], ["user_id", 1], ["created_at", "2019-01-13 21:57:08.172483"], ["updated_at", "2019-01-13 21:57:08.172483"]]
(0.6ms) COMMIT
Redirected to http://localhost:3000/coffeeshops/new-shop-9c762d25-77e4-499a-b009-1ad150515dbb
Completed 302 Found in 26ms (ActiveRecord: 7.2ms)
did you restart the server after rule settings? may be that is basic reason. I have faced same issu and solved like this way.
rails s
Related
Using Rails 6 and CanCanCan. Here are my models:
class Shop < ApplicationRecord
has_many :shop_reviews, dependent: :destroy
end
class ShopReview < ApplicationRecord
belongs_to :shop, counter_cache: true
belongs_to :user_profile, counter_cache: true
end
class User < ApplicationRecord
has_one :user_profile, dependent: :destroy
has_many :shop_reviews, through: :user_profile
end
class UserProfile < ApplicationRecord
belongs_to :user
has_many :shop_reviews
end
ShopReview table:
id, shop_id, user_profile_id, review
Association:
1. A shop can have many reviews
2. Only one User Profile can have one review on a shop
Abilities I want to define:
1. User Profile can edit, update, destroy his own review of a shop
2. User Profile cannot create a new review if a review of his exist on that shop
What I tried:
class Ability
include CanCan::Ability
def initialize(user)
can :read, :all
if user.present?
can [:update, :destroy], ShopReview, user_profile_id: user.user_profile.id
can :create, ShopReview do
!ShopReview.exists?(user_profile_id: user.user_profile.id, shop_id: :shop_id)
end
end
end
end
But I can't seem to pass in the shop_id on visiting /shops/11/shop_reviews/new. Here's the log:
Started GET "/shops/11/shop_reviews/new" for 127.0.0.1 at 2020-01-04 00:23:10 +0800
Processing by ShopReviewsController#new as HTML
Parameters: {"shop_id"=>"11"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 9], ["LIMIT", 1]]
UserProfile Load (0.2ms) SELECT "user_profiles".* FROM "user_profiles" WHERE "user_profiles"."user_id" = $1 LIMIT $2 [["user_id", 9], ["LIMIT", 1]]
↳ app/models/ability.rb:9:in `initialize'
ShopReview Exists? (0.3ms) SELECT 1 AS one FROM "shop_reviews" WHERE "shop_reviews"."user_profile_id" = $1 AND "shop_reviews"."shop_id" = $2 LIMIT $3 [["user_profile_id", 8], ["shop_id", nil], ["LIMIT", 1]]
↳ app/models/ability.rb:11:in `block in initialize'
Shop Load (0.2ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = $1 ORDER BY "shops"."created_at" DESC LIMIT $2 [["id", 11], ["LIMIT", 1]]
You should use gem pundit to create authorizations in your app. Pundit is easy to scale than gem cancancan. Because pundit allows your app to create authorization on each model. You can search more about two gems and choose what is most suitable for you.
I have a User model and a Spkr model.
User:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :tlks, dependent: :destroy
has_many :spkrs, dependent: :destroy
has_many :msgs, dependent: :destroy
has_one_attached :image
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
def slug_candidates
[
:username,
[:username, DateTime.now.to_date]
]
end
end
Spkr
class Spkr < ApplicationRecord
belongs_to :tlk
belongs_to :user
has_many :msgs, dependent: :destroy
has_one_attached :image
end
When my user model has an image attached, when a spkr is made I want it to have the same image attached to it as the user generating the spkr.
I have a SpkrMaker module:
module SpkrMaker
def make_spkr
spkr = Spkr.create!(
user: current_user,
tlk: #tlk,
name: current_user.username,
bio: current_user.bio,
)
if current_user.image.present?
ActiveStorage::Attachment.create(
name: 'image',
record_type: 'Spkr',
record_id: spkr.id,
blob_id: current_user.image.id
)
end
end
end
This is called during the flow, when it is called my server logs state:
Started POST "/tlks" for ::1 at 2020-01-11 10:39:41 +0000
Processing by TlksController#create as JS
Parameters: {"authenticity_token"=>"1NJc0ZSwo8hL1JHw5karkWNxoWRzHPNx/xecaAQHdN+EdsG/o+yZwdxZXLZLPVbkgiPiZZX6PpaF38VX5etTAw==", "tlk"=>{"title"=>"goooolan"}, "commit"=>"Create Tlk"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 23], ["LIMIT", 1]]
(0.1ms) begin transaction
↳ app/controllers/tlks_controller.rb:34:in `create'
Tlk Exists? (0.3ms) SELECT 1 AS one FROM "tlks" WHERE "tlks"."id" IS NOT NULL AND "tlks"."slug" = ? LIMIT ? [["slug", "goooolan"], ["LIMIT", 1]]
↳ app/controllers/tlks_controller.rb:34:in `create'
Tlk Create (0.4ms) INSERT INTO "tlks" ("user_id", "title", "created_at", "updated_at", "slug", "invite_code") VALUES (?, ?, ?, ?, ?, ?) [["user_id", 23], ["title", "goooolan"], ["created_at", "2020-01-11 10:39:41.866979"], ["updated_at", "2020-01-11 10:39:41.866979"], ["slug", "goooolan"], ["invite_code", 469667]]
↳ app/controllers/tlks_controller.rb:34:in `create'
(0.9ms) commit transaction
↳ app/controllers/tlks_controller.rb:34:in `create'
(0.1ms) begin transaction
↳ lib/spkr_maker.rb:3:in `make_spkr'
Spkr Create (0.5ms) INSERT INTO "spkrs" ("tlk_id", "user_id", "name", "bio", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["tlk_id", 135], ["user_id", 23], ["name", "test"], ["bio", "info about me"], ["created_at", "2020-01-11 10:39:41.871219"], ["updated_at", "2020-01-11 10:39:41.871219"]]
↳ lib/spkr_maker.rb:3:in `make_spkr'
(0.8ms) commit transaction
↳ lib/spkr_maker.rb:3:in `make_spkr'
ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 23], ["record_type", "User"], ["name", "image"], ["LIMIT", 1]]
↳ lib/spkr_maker.rb:9:in `make_spkr'
(0.0ms) begin transaction
↳ lib/spkr_maker.rb:10:in `make_spkr'
Spkr Load (0.2ms) SELECT "spkrs".* FROM "spkrs" WHERE "spkrs"."id" = ? LIMIT ? [["id", 104], ["LIMIT", 1]]
↳ lib/spkr_maker.rb:10:in `make_spkr'
ActiveStorage::Blob Load (0.0ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ? [["id", 51], ["LIMIT", 1]]
↳ lib/spkr_maker.rb:10:in `make_spkr'
(0.0ms) rollback transaction
↳ lib/spkr_maker.rb:10:in `make_spkr'
Completed 422 Unprocessable Entity in 25ms (ActiveRecord: 3.7ms | Allocations: 14233)
ActiveRecord::RecordInvalid (Validation failed: Blob must exist):
lib/spkr_maker.rb:10:in `make_spkr'
app/controllers/tlks_controller.rb:36:in `create
When I run User.last.image in the rails console, I get the following:
irb(main):002:0> User.last.image
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<ActiveStorage::Attached::One:0x00007f88a21cda90 #name="image", #record=#<User id: 23, email: "j#test.com", username: "test", bio: "info about me", name: nil, created_at: "2020-01-11 01:56:22", updated_at: "2020-01-11 01:56:48", slug: "test">>
irb(main):003:0>
I do not know what the problem is, and am not good enough at understanding the server logs to work out what is going wrong.
A Spkr is made during the process, so everything above line 10 is working in the SpkrMaker module (ActiveStorage::Attachment.create( = line 10).
OK Further information as of this morning...(11/01/2020)
irb(main):010:0> User.last.image.id
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
ActiveStorage::Attachment Load (0.2ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 23], ["record_type", "User"], ["name", "image"], ["LIMIT", 1]]
=> 51
But
irb(main):011:0> ActiveStorage::Blob.last.id
ActiveStorage::Blob Load (0.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" ORDER BY "active_storage_blobs"."id" DESC LIMIT ? [["LIMIT", 1]]
=> 49
I don't know why there is this difference.
While attaching the image to Spkr you are tagging active storage attachment id of current_user instead of blob_id instead of blob id I have update the code.
module SpkrMaker
def make_spkr
spkr = Spkr.create!(
user: current_user,
tlk: #tlk,
name: current_user.username,
bio: current_user.bio,
)
if current_user.image.present?
ActiveStorage::Attachment.create(
name: 'image',
record_type: 'Spkr',
record_id: spkr.id,
blob_id: current_user.image.blob_id
)
end
end
end
It should work now.
In my controllers/common/roles_controller.rb I would like to check if particular role (ID) belongs to current_user company users and if not, redirect to errors_path:
def correct_role
role_user = Role.where(:id => params[:id]).select('user_id').first
company_user = current_user.companies.includes(:users)
redirect_to(errors_path) unless company_user.include? role_user.id
end
Definitions:
role_user - finds user ID for particular role ID ("user_id" is column of roles table)
company_user - finds all user ID who belong to companies, which belong to current_user
models/role.rb
belongs_to :user, optional: true, inverse_of: :roles
accepts_nested_attributes_for :user
enum general: { seller: 1, buyer: 2, seller_buyer: 3}, _suffix: true
enum dashboard: { denied: 0, viewer: 1, editer: 2, creater: 3, deleter: 4}, _suffix: true
models/user.rb
#User has roles
has_many :roles
accepts_nested_attributes_for :roles, reject_if: proc { |attributes| attributes[:name].blank? }
# User has many companies
has_many :accounts, dependent: :destroy
has_many :companies, through: :accounts
models/account.rb
class Account < ApplicationRecord
belongs_to :company
belongs_to :user
accepts_nested_attributes_for :company, :user
end
models/company.rb
has_many :accounts, dependent: :destroy
has_many :users, through: :accounts
At the moment with role_user and company_user I can find both ID, however I cannot do the checking part. How do I do that correctly, please? Thank you for any help!
Update
#sajan code give this in console when I open /common/roles/1/edit (current_user ID=1 and should be allowed to edit):
Parameters: {"id"=>"1"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.6ms) SELECT COUNT(*) FROM "companies" INNER JOIN "accounts" ON "companies"."id" = "accounts"."company_id" WHERE "accounts"."user_id" = ? [["user_id", 1]]
(0.3ms) SELECT "roles".id FROM "roles" WHERE "roles"."user_id" = ? [["user_id", 1]]
#Abhishek Kumar code in console gives:
Parameters: {"id"=>"1"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Company Load (0.5ms) SELECT "companies".* FROM "companies" INNER JOIN "accounts" ON "companies"."id" = "accounts"."company_id" WHERE "accounts"."user_id" = ? [["user_id", 1]]
(0.4ms) SELECT "users".id FROM "users" INNER JOIN "accounts" ON "users"."id" = "accounts"."user_id" WHERE "accounts"."company_id" = ? [["company_id", 13]]
Role Load (0.2ms) SELECT "roles"."user_id" FROM "roles" WHERE "roles"."id" = ? ORDER BY "roles"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Update v2
So I'm trying to use this code:
def correct_role
company_user_ids = current_user.companies.map(&:user_ids)
role_user = Role.where(:id => params[:id]).select('user_id').first
unless role_user.user_id.in?(company_user_ids)
redirect_to(errors_path)
end
end
however it redirects to errors_path in any case, this is what I have in console:
Parameters: {"id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Company Load (0.5ms) SELECT "companies".* FROM "companies" INNER JOIN "accounts" ON "companies"."id" = "accounts"."company_id" WHERE "accounts"."user_id" = ? [["user_id", 1]]
(0.4ms) SELECT "users".id FROM "users" INNER JOIN "accounts" ON "users"."id" = "accounts"."user_id" WHERE "accounts"."company_id" = ? [["company_id", 13]]
Role Load (0.3ms) SELECT "roles"."user_id" FROM "roles" WHERE "roles"."id" = ? ORDER BY "roles"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
It seems that a better logic would be to define on the model for the current_user an association through companies and users to roles.
Then you can check:
def correct_role
redirect_to(errors_path) unless current_user.company_user_roles.where(id: params[:id]).exists?
end
It would be a single SQL statement that would return zero or one rows, and execute very quickly with the appropriate indexes in place.
Edit:
To company.rb, add:
has_many :user_roles, through: :users, source: :roles
To user.rb (assuming that current_user is an instance of this model) add:
has_many :company_user_roles, through: :companies, source: :user_roles
Maybe you could do like this:
def correct_role
unless current_user.companies.count > 0 && current_user.role_ids.include?(params[:id])
redirect_to(errors_path)
end
end
I'm new to rails and I want to know how to fetch a one-to-one relationship. I want to fetch users city. In my postgresql database I have:
cities Table:
city:varchar
zipcode: integer
users Table
name:varchar
city_id:int
and in city and user model I have:
class City < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :city
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
I tried the following in my search controller but didnt work, when logged in:
current_user.city
I get the following error
Processing by SearchController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>"", "criteria"=>"1", "commit"=>"Search"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 ORDER BY "users"."id" ASC LIMIT 1
PG::UndefinedColumn: ERROR: column cities.user_id does not exist
LINE 1: SELECT "cities".* FROM "cities" WHERE "cities"."user_id" =...
^
: SELECT "cities".* FROM "cities" WHERE "cities"."user_id" = $1 LIMIT 1
Completed 500 Internal Server Error in 11ms
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column cities.user_id does not exist
LINE 1: SELECT "cities".* FROM "cities" WHERE "cities"."user_id" =...
^
: SELECT "cities".* FROM "cities" WHERE "cities"."user_id" = $1 LIMIT 1):
why am I suppose to add a user_id column to cities table, when I have cities foreign key in users table? I dont want to add user_id into cities table.
You can use has_one :through association with join table. Some example for you below.
user model:
class User < ActiveRecord::Base
has_one :city, through: :user_city
has_one :user_city
end
city model:
class City < ActiveRecord::Base
belongs_to :user
end
user city join model:
class UserCity < ActiveRecord::Base
belongs_to :city
belongs_to :user
end
migration for join tables:
class JoinUserCity < ActiveRecord::Migration
def change
create_table :user_cities do |t|
t.integer :user_id
t.integer :city_id
end
end
end
Test in rails console:
=> u = User.create
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-12-07 15:47:14.595728"], ["updated_at", "2014-12-07 15:47:14.595728"]]
(3.3ms) commit transaction
=> #<User id: 4, created_at: "2014-12-07 15:47:14", updated_at: "2014-12-07 15:47:14">
=> u.city
City Load (0.2ms) SELECT "cities".* FROM "cities" INNER JOIN "user_cities" ON "cities"."id" = "user_cities"."city_id" WHERE "user_cities"."user_id" = ? LIMIT 1 [["user_id", 4]]
=> nil
=> c = City.create
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "cities" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-12-07 15:47:24.535039"], ["updated_at", "2014-12-07 15:47:24.535039"]]
(3.3ms) commit transaction
=> #<City id: 1, created_at: "2014-12-07 15:47:24", updated_at: "2014-12-07 15:47:24">
irb(main):004:0> u.city = c
UserCity Load (0.3ms) SELECT "user_cities".* FROM "user_cities" WHERE "user_cities"."user_id" = ? LIMIT 1 [["user_id", 4]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "user_cities" ("city_id", "user_id") VALUES (?, ?) [["city_id", 1], ["user_id", 4]]
(1.0ms) commit transaction
=> #<City id: 1, created_at: "2014-12-07 15:47:24", updated_at: "2014-12-07 15:47:24">
irb(main):005:0> u.save
(0.1ms) begin transaction
(0.1ms) commit transaction
=> true
=> u = User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 4, created_at: "2014-12-07 15:47:14", updated_at: "2014-12-07 15:47:14">
=> u.city
City Load (0.2ms) SELECT "cities".* FROM "cities" INNER JOIN "user_cities" ON "cities"."id" = "user_cities"."city_id" WHERE "user_cities"."user_id" = ? LIMIT 1 [["user_id", 4]]
=> #<City id: 1, created_at: "2014-12-07 15:47:24", updated_at: "2014-12-07 15:47:24">
take a look at the document of has_one and belogns_to,
belongs_to(name, options = {})
Specifies a one-to-one association with another class. This method should only be used if this class
contains the foreign key. If the other class contains the foreign key, then you should use has_one
instead.
as the user table has the foreign key, you should change your model definition like this
class City < ActiveRecord::Base
has_one :user
end
class User < ActiveRecord::Base
belongs_to :city
end
I have found some similar questions on stack overflow, but nothing that helps. I have a user model. I have a profile model that belongs to the user model. I have a job model that belongs to the profile model. I am making a simple form to create a job. When i submit the form in the browser, I am given the error:
undefined method `build_job' for #<Student:0x007f8309023530>
And it shows the create action in the jobs controller:
def create
job = current_user.build_job(job_params)
job.save
redirect_to profile_path(current_user.profile_name)
end
The jobs create method is identical to the profiles create method, with the word profile replaced with job, so I can't figure out why its not working. My guess is it has something to do with jobs belonging to a model that belongs to another model. How do I fix this? Also, here is the job_params method:
def profile_params
params.require(:profile).permit(:title, :category, :description, :state, :zip_code, :rate, jobs_attributes: [:firm, :position])
end
And here are my models:
Job:
class Job < ActiveRecord::Base
belongs_to :profile
end
Profile:
class Profile < ActiveRecord::Base
belongs_to :user
has_many :jobs, :dependent => :destroy
end
User:
class User < ActiveRecord::Base
has_one :profile
end
Reference in view:
<%= #user.profile.job.firm if #user.profile.try(:job)%>
I am also adding my server log from clicking on submit. Hope it helps answer the question:
Started POST "/jobs" for 127.0.0.1 at 2013-10-27 21:45:06 -0400
ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by JobsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VRqIuzR1x6tE/G+/wzrG1iFBOEDE7mgsfyjokX7wNZo=", "job"=>{"firm"=>"signat", "position"=>""}, "commit"=>"Save"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
(0.2ms) BEGIN
SQL (3.4ms) INSERT INTO "jobs" ("created_at", "firm", "position", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Mon, 28 Oct 2013 01:45:06 UTC +00:00], ["firm", "signat"], ["position", ""], ["updated_at", Mon, 28 Oct 2013 01:45:06 UTC +00:00]]
(0.4ms) COMMIT
Redirected to http://localhost:3000/profiles/philip7899
Completed 302 Found in 209ms (ActiveRecord: 9.8ms)
Started GET "/profiles/philip7899" for 127.0.0.1 at 2013-10-27 21:45:06 -0400
Processing by ProfilesController#show as HTML
Parameters: {"id"=>"philip7899"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."profile_name" = 'philip7899' ORDER BY "users"."id" ASC LIMIT 1
Profile Load (0.8ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 1]]
School Load (0.9ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 ORDER BY "schools"."id" ASC LIMIT 1 [["id", 1]]
Job Exists (0.6ms) SELECT 1 AS one FROM "jobs" WHERE "jobs"."profile_id" = $1 LIMIT 1 [["profile_id", 1]]
Rendered profiles/_full_profile.html.erb (92.4ms)
Rendered profiles/show.html.erb within layouts/application (95.4ms)
Rendered layouts/_ssi_header_inner.html.erb (4.2ms)
Rendered layouts/_ssi_footer.html.erb (0.2ms)
Completed 200 OK in 218ms (Views: 209.4ms | ActiveRecord: 5.9ms)
Couple of problems. You need to define has_many jobs on user, and the correct method for building has_many is association.build not build_association:
class User
has_many :jobs, through: :profile
end
job = current_user.jobs.build(job_params)