Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
smug forum asshole
Jan 15, 2005

smug forum rear end in a top hat posted:

association stupidity

If anyone is curious, I ended up going with the following, and it seems to have been the right choice:
code:
class Group < ActiveRecord::Base
  has_many :posts
  has_many :users, :through => :group_memberships
  has_many :group_memberships
end

class Post < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :group_memberships
  has_many :groups, :through => :group_memberships
  has_many :posts
end

class GroupMembership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end
I feel like I understand associations a bit better now. Thanks.

Adbot
ADBOT LOVES YOU

Pardot
Jul 25, 2001




smug forum rear end in a top hat posted:

If anyone is curious, I ended up going with the following, and it seems to have been the right choice

I feel like I understand associations a bit better now. Thanks.

Post your schema.rb file, please. I want to either congratulate you for knowing to add indicies on the join columns, or tell you to do that if you haven't.

skidooer
Aug 6, 2001

Pardot posted:

I want to either congratulate you for knowing to add indicies on the join columns
Or congratulate Rails. It automatically adds indexes on join columns when you create your model. :)

smug forum asshole
Jan 15, 2005

Pardot posted:

Post your schema.rb file, please. I want to either congratulate you for knowing to add indicies on the join columns, or tell you to do that if you haven't.

code:
ActiveRecord::Schema.define(:version => 20110713203542) do

  create_table "group_memberships", :force => true do |t|
    t.integer  "group_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "groups", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
    t.integer  "group_id"
  end

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "password_hash"
    t.string   "password_salt"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end
So yeah, I do not have indexes anywhere. :geno: Never thought about it. Should I just add a migration with indexes for user_id and group_id, then?

NotShadowStar
Sep 20, 2000

skidooer posted:

Or congratulate Rails. It automatically adds indexes on join columns when you create your model. :)

Wow, since when? I've been doing it in the migration file for years now.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!

skidooer posted:

Or congratulate Rails. It automatically adds indexes on join columns when you create your model. :)

Is this new in 3.1 or something? I don't remember it happening on 3.0.8. I assume you have to use :references as your column type, but is there any other trickery needed?

enki42 fucked around with this message at 14:22 on Jul 14, 2011

Cock Democracy
Jan 1, 2003

Now that is the finest piece of chilean sea bass I have ever smelled
Paperclip question: I have a model where I want the user to upload a file, then a resque worker does some things to the file and saves a finished version of the file. I want to keep both the original and finished files, so I think I need two paperclip fields. The question is, how to do I tell paperclip to create the finished file when I have the contents in a variable? I can only find info on how to create the file from a form submission.

edit: Maybe it's OK to set the finished file's file_name then write to that file directly?

Cock Democracy fucked around with this message at 16:44 on Jul 14, 2011

skidooer
Aug 6, 2001

enki42 posted:

Is this new in 3.1 or something?
Oh, maybe. I was thinking it was older than that, but it is present in 3.1 for sure. And yes, you have to tell it that it is a reference column, but that's all.

rugbert
Mar 26, 2003
yea, fuck you
So I have my models set up as sections => has_many pages and 2 sections have a boolean field called has_bio set to 1 where the rest are 0.

Im trying to check the current page's parent section to see if the has_bio field is set to true.
So why does this always evaluate as true?
code:
  <% if @page.section.has_bio == true %>
    <% create_variable(:mce_link_list, ajax_bio_links_path(@section))  %>
  <% else %>
    <% create_variable(:mce_link_list, ajax_links_path())  %>
  <% end %>

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

This is almost a cross-post from the SQL, thread, but how do you guys balance your hardware resources between your web and DB server? We're running Ruby with Apache/Passenger (should we look into nginx or something else?)

My main question is should our web server have 12GB of vRAM, would it better used for our DB server?

The server is has 3 vCPU's, Xeon L5520 @ 2.27GHz. The Ruby/Apache server has 5, and also has 12GB. Wouldn't it make more sense to give the DB server more RAM and maybe equal out the CPU's? Not sure if it's a single quad with HT enabled, or a dual-quad with HT disabled. Maybe we should enable HT and give each server 8 vCPU?

Any tips on my.cnf settings for InnoDB?

Going from this site:

http://www.mysqlperformanceblog.com...b-memory-usage/

I've decided to change the following settings on our server.
code:

innodb_buffer_pool_size=8096M
innodb_flush_method=O_DIRECT
innodb_thread_concurrency=6
innodb_flush_log_at_trx_commit=2
innodb_log_buffer_size=4M

cat /proc/meminfo on the server shows:

MemTotal: 12299960 kB
MemFree: 2467744 kB

Our ibdata1 file is 16G

manero
Jan 30, 2006

Bob Morales posted:

My main question is should our web server have 12GB of vRAM, would it better used for our DB server?

The server is has 3 vCPU's, Xeon L5520 @ 2.27GHz. The Ruby/Apache server has 5, and also has 12GB. Wouldn't it make more sense to give the DB server more RAM and maybe equal out the CPU's? Not sure if it's a single quad with HT enabled, or a dual-quad with HT disabled. Maybe we should enable HT and give each server 8 vCPU?


If you can swing it, install Ganglia and see what sort of usage the app and DB servers have. It's not a hard and fast rule but CPU and disk speed are better on the DB machines, and depending, CPU and lots of RAM on the app servers. Obviously different circumstances can change that, but the best way to know for sure is to measure.

Pardot
Jul 25, 2001




Bob Morales posted:

InnoDB?

Is there a reason you're using mysql instead of postgres?

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Pardot posted:

Is there a reason you're using mysql instead of postgres?

No clue, I've only been here a few months. Their database originates back to when they were an ASP shop.

I changed the DB stuff around this morning, don't notice any difference but I'll get a better idea on Monday since we're really only busy M-F 8am-8pm.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


Any of you fine folks doing something for the Rails 3.1 Hackfest next week?

NotShadowStar
Sep 20, 2000
I really want to go to the one in Chicago but all the blog says is 'It is at Hashrocket!' and Hashrocket has no information posted :mad:

8ender
Sep 24, 2003

clown is watching you sleep
I really want to go to the Toronto one but damned if I'm missing paintball day.

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe
Datamapper is pretty awesome guys; yes it involved switching my rails 3.x site from authlogic to devise, but that was minimal :effort:. No migrations, define properties once, auto migrate/upgrade... utterly awesome for development. Thought you should know!

:ninja: edit - plus, its faster and much nicer to use than AR.

Evil Trout
Nov 16, 2004

The evilest trout of them all

kalleth posted:

Datamapper is pretty awesome guys; yes it involved switching my rails 3.x site from authlogic to devise, but that was minimal :effort:. No migrations, define properties once, auto migrate/upgrade... utterly awesome for development. Thought you should know!

:ninja: edit - plus, its faster and much nicer to use than AR.

I spent almost a year working on a series of dating sites that used DataMapper, and let me tell you it was like pulling teeth. We had to make awful choices like sticking with a version that had known memory leaks or spending months upgrading to the latest version because they changed the API in huge awful ways.

Also, the killer feature of DM at the time, the Identity Map, doesn't work when you go across multiple databases.

If you find a bug in it, or have a question, good luck finding anyone to answer you. The IRC room would have 30 lurkers and nobody seemingly working on it or around at any time to answer questions.

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe

Evil Trout posted:

I spent almost a year working on a series of dating sites that used DataMapper, and let me tell you it was like pulling teeth. We had to make awful choices like sticking with a version that had known memory leaks or spending months upgrading to the latest version because they changed the API in huge awful ways.

Also, the killer feature of DM at the time, the Identity Map, doesn't work when you go across multiple databases.

If you find a bug in it, or have a question, good luck finding anyone to answer you. The IRC room would have 30 lurkers and nobody seemingly working on it or around at any time to answer questions.

Not my experience. The killer features of DM for me are only defining data model once (in the models) and proper o(x) scalability. Also, had issues moving AR to DM and theirc channel was more than helpful, providing that is you use Google and read docs first.

Evil Trout
Nov 16, 2004

The evilest trout of them all

kalleth posted:

Not my experience. The killer features of DM for me are only defining data model once (in the models) and proper o(x) scalability. Also, had issues moving AR to DM and theirc channel was more than helpful, providing that is you use Google and read docs first.

I think if you end up using DM for a large scale project you will change your tune.

Auto-migrations are nice while developing a small site on your own machine, but completely impractical on a production system when you want to control exactly when migrations occur. For that almost everyone uses dm-migrations and then you are declaring data in more than one place.

And the o(n) issue only really works on naive queries. And like I said it doesn't work at all if you have more than one database (such as in the really common master/slave build of mysql).

Glad to hear they're around in IRC. I would just never trust a project that made such awful changes to backwards compatibility again. In one point version, calling bang methods (create!) would raise exceptions if the validations failed like rails. In the next release, calling the bang methods would avoid hooks. Completely different behaviour!

We strongly suspected that we were the largest site in the world using DM as most people you hear championing it are working on weekend hack projects and don't have responses for these serious issues.

NotShadowStar
Sep 20, 2000

kalleth posted:

Not my experience. The killer features of DM for me are only defining data model once (in the models) and proper o(x) scalability. Also, had issues moving AR to DM and theirc channel was more than helpful, providing that is you use Google and read docs first.

If these things are really what you're after then look at MongoDB and Mongoid.

Stup
Mar 19, 2009
Been developing in Rails for a few months now and ran into some difficulties with this problem.

I'm trying to customize the error messages which are displayed from the Model. I asked around on stackoverflow but couldn't get anywhere with the suggestions. Currently I verify that there are no duplicated Members when trying to create a new Member and add it to a Team.


members_controller.rb
code:
def create
  @team = current_team
  player = Player.find(params[:player_id])
  @member = @team.add_player(player.id)

  respond_to do |format|
     if @member.save
        format.html { redirect_to(@team, :notice => 'Member was successfully added.') }
        format.js { @current_member = @member }
        format.xml  { render :xml => @member, 
        :status => :created, :location => @member } 
     else
        format.html { redirect_to(@team, :notice => 'Member already exists.') }
        format.xml  { render :xml => @member.errors, 
        :status => :unprocessable_entity }
     end
   end
 end
team.rb
code:
def add_player(player_id)
    current_member = members.build(:player_id => player_id)
    current_member
end
I want to add some logic to my add_player method in team.rb that checks various properties of the player that is being added. This action will require multiple failure messages, other than 'Member already exists.' I've already got some methods in team.rb that return the values I want to check. I.e. Method position_count which counts and returns the number of players that fit a certain condition.

Really appreciate any help I can get!

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe

Stup posted:

Been developing in Rails for a few months now and ran into some difficulties with this problem.

I'm trying to customize the error messages which are displayed from the Model. I asked around on stackoverflow but couldn't get anywhere with the suggestions. Currently I verify that there are no duplicated Members when trying to create a new Member and add it to a Team.


members_controller.rb


team.rb

I want to add some logic to my add_player method in team.rb that checks various properties of the player that is being added. This action will require multiple failure messages, other than 'Member already exists.' I've already got some methods in team.rb that return the values I want to check. I.e. Method position_count which counts and returns the number of players that fit a certain condition.

Really appreciate any help I can get!

You're going to do one of two things here.

The first is to pull the entire validation down to the model - preferred approach - using a custom validator (http://edgeguides.rubyonrails.org/active_record_validations_callbacks.html 6. Creating Custom Validation Methods)

i.e. in the model
code:
validate :method_name_as_symbol

def method_name_as_symbol
errors.add :member, "must be unique" if Members.find(member_id) or w/e
end
Within that method, you can either use errors.add(:fieldname, "message"), or alternatively, errors.add_to_base - see http://ar.rubyonrails.org/classes/ActiveRecord/Errors.html#M000302 for more info.

Alternatively, you can always do @model.errors.add_to_base in your controller, but I'd push the validation to the model...

Stup
Mar 19, 2009
So in this case your :fieldname is an attribute of the team no? What if I'm using a Team method that is checking an attribute of the Member. Is there a way for this to check something that isn't a column in Team?

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe
Yes, there is; you'd use a custom validator with validates :validationmethod in the model. So, for example

code:
class Team

 validates :cant_have_members_with_same_nickname

 def cant_have_members_with_same_nickname
   nicks = members.map { |m| m.nickname }
   unless nicks.uniq.length == nicks.length
     errors.add_to_base "Can't have members with duplicate nicknames"
     return false
   end
 end

end

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come

Doc Hawkins posted:

Any of you fine folks doing something for the Rails 3.1 Hackfest next week?
I'm gearing up to upgrade my company's app to 3.1 anyways, so being able to really bug the hell out of core people when I run into any problems appeals to me. Deeply.

8ender
Sep 24, 2003

clown is watching you sleep

dustgun posted:

I'm gearing up to upgrade my company's app to 3.1 anyways, so being able to really bug the hell out of core people when I run into any problems appeals to me. Deeply.

Any idea if 3.1 is going to be dramatically different than 3.0? I'm in the same boat and just to see how bad things were I upgraded one of our 2.3.11 apps yesterday and it was oddly painless. I think I changed one or two lines of code but everything just worked. I was kind of expecting apocalyptic app failure across the board but it doesn't look like all that much changed in the core MVC parts of rails.

Stup
Mar 19, 2009

kalleth posted:

Yes, there is; you'd use a custom validator with validates :validationmethod in the model. So, for example

code:
class Team

 validates :cant_have_members_with_same_nickname

 def cant_have_members_with_same_nickname
   nicks = members.map { |m| m.nickname }
   unless nicks.uniq.length == nicks.length
     errors.add_to_base "Can't have members with duplicate nicknames"
     return false
   end
 end

end

This makes sense. I guess my Ruby isn't strong enough and I'm not quite sure how to build my conditional. Would you happen to know any good resources for me to read up on this?

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe

Stup posted:

This makes sense. I guess my Ruby isn't strong enough and I'm not quite sure how to build my conditional. Would you happen to know any good resources for me to read up on this?

Depends what you mean by 'conditional'. You mean your 'question'/'if statement' ? Most of the time when operating across a set your options are either each, map, or inject, depending on what the problem is.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


8ender posted:

Any idea if 3.1 is going to be dramatically different than 3.0? I'm in the same boat and just to see how bad things were I upgraded one of our 2.3.11 apps yesterday and it was oddly painless. I think I changed one or two lines of code but everything just worked. I was kind of expecting apocalyptic app failure across the board but it doesn't look like all that much changed in the core MVC parts of rails.

I've heard horror stories, but in direct experience, it seems surprisingly backwards-compatible.

I guess my apps just weren't sophisticated enough to break. :shobon:

Stup
Mar 19, 2009

kalleth posted:

Depends what you mean by 'conditional'. You mean your 'question'/'if statement' ? Most of the time when operating across a set your options are either each, map, or inject, depending on what the problem is.

For example, I can't understand the syntax used here:
code:
nicks = members.map { |m| m.nickname }

kalleth
Jan 28, 2006

C'mon, just give it a shot
Fun Shoe

Stup posted:

For example, I can't understand the syntax used here:
code:
nicks = members.map { |m| m.nickname }

That's just a short way of doing a single-line loop in Ruby.

It's the equivalent of doing

code:
nicks = members.map do  |member|
  member.nickname
end
.map is a rubyist way of saying 'for each item in this enumerable, populate the output array with the value the following block evaluates to'

For example, for:
array = [{:value => "one"}, {:value => "two"}]

(an array of two hashes each with a :value element)
code:
array.map do |element|
  element[:value]
end
returns:
["one","two"]
Which is identical to doing:
code:
array.map {|e| e[:value]}
Ruby people like one-liners :)

Best starting point for that kind of thing is the ruby Enumerable docs - http://www.ruby-doc.org/core/classes/Enumerable.html

kalleth fucked around with this message at 00:21 on Jul 18, 2011

Pardot
Jul 25, 2001




Stup posted:

For example, I can't understand the syntax used here:
code:
nicks = members.map { |m| m.nickname }

Give _why's poignant guide to ruby a read.

Stup
Mar 19, 2009
Thanks for all your help! Off to do a little reading now.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

NotShadowStar posted:

I really want to go to the one in Chicago but all the blog says is 'It is at Hashrocket!' and Hashrocket has no information posted :mad:

http://hashrocket.com/contact-us

Their office isn't super-big, but there's definitely room for maybe a dozen people there.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

Doc Hawkins posted:

I've heard horror stories, but in direct experience, it seems surprisingly backwards-compatible.

I guess my apps just weren't sophisticated enough to break. :shobon:

In general, you should avoid making apps sophisticated enough to break. Business logic stored in models is generally safe, calling model methods from your controllers is safe, view fuckery and diddling with load paths is not.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Stup posted:

Thanks for all your help! Off to do a little reading now.

BTW you probably don't need a custom validator, For instance, you can just do
code:
validates_uniqueness_of :nickname, :scope => [:team_id]
That's totally a blind guess at what your models look like but hopefully you get the idea. Unless I'm misunderstanding what you're trying to do it's unlikely you'll want or need to get a list of all team members and compare their nicknames to the new nick; let AR handle that for you.

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!

kalleth posted:

Ruby people like one-liners :)

Better still is:

code:
nicks = members.map(&:nickname)

enki42
Jun 11, 2001
#ATMLIVESMATTER

Put this Nazi-lover on ignore immediately!

8ender posted:

I really want to go to the Toronto one but damned if I'm missing paintball day.

If you do manage to come, say hi. I'm Ryan, I'm running the event.

Adbot
ADBOT LOVES YOU

8ender
Sep 24, 2003

clown is watching you sleep

enki42 posted:

If you do manage to come, say hi. I'm Ryan, I'm running the event.

drat, now I really wish I could come, especially since I have a developer here thats learning the ropes on Ruby and Rails after years of PHP and this would be some good experience. Unfortunately that developer is the owner of the farm we're all paintballing at.

Its also a bit of a drive as we're all pretty far out in the countryside. Rural Ontario Ruby developers :banjo:

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply